php - 我无法迁移 laravel 项目
问题描述
我在打开 laravel 项目时遇到问题,因为我无法进行 php artisan 迁移。它显示以下错误:
Illuminate\Database\QueryException
SQLSTATE[42S02]: Base table or view not found: 1146 Table 'techdb.settings' doesn't exist (SQL: select `key`, `value` from `settings`)
at C:\xampp\htdocs\laravel\laravel-pos\vendor\laravel\framework\src\Illuminate\Database\Connection.php:671
667| // If an exception occurs when attempting to run a query, we'll format the error
668| // message to include the bindings with SQL, which will make this exception a
669| // lot more helpful to the developer instead of just the database's errors.
670| catch (Exception $e) {
> 671| throw new QueryException(
672| $query, $this->prepareBindings($bindings), $e
673| );
674| }
675|
• A table was not found: You might have forgotten to run your migrations. You can run your migrations using `php artisan migrate`.
https://laravel.com/docs/master/migrations#running-migrations
1 [internal]:0
Illuminate\Foundation\Application::Illuminate\Foundation\{closure}(Object(App\Providers\AppServiceProvider))
2 C:\xampp\htdocs\laravel\laravel-pos\vendor\laravel\framework\src\Illuminate\Database\Connection.php:331
PDOException::("SQLSTATE[42S02]: Base table or view not found: 1146 Table 'techdb.settings' doesn't exist")
PS C:\xampp\htdocs\laravel\laravel-pos>
我已经在 mysql 中创建了一个数据库并更新了环境,但它不会迁移。当它在错误中说“techdb.settings”不存在时,它指的是什么?
我还在提供程序文件夹中找到了这个。这可能是问题所在?
<?php
namespace App\Providers;
use App\Models\Setting;
use Illuminate\Support\ServiceProvider;
class AppServiceProvider extends ServiceProvider
{
/**
* Register any application services.
*
* @return void
*/
public function register()
{
//
}
/**
* Bootstrap any application services.
*
* @return void
*/
public function boot()
{
// 'key' => 'value'
$settings = Setting::all('key', 'value')
->keyBy('key')
->transform(function ($setting) {
return $setting->value;
})
->toArray();
config([
'settings' => $settings
]);
config(['app.name' => config('settings.app_name')]);
}
}
解决方案
首先,这不是一个解决方案,每次你刷新数据库并且你必须评论它或者你的队友想要在他的系统上安装项目他不知道你做了什么以及在哪里做了。所以它可能很混乱..所以,当您要迁移时,引导方法会搜索数据库中不存在的设置表,这就是它引发错误的原因。所以你能做什么,你可以把它绑定一个 try-catch 像 follow。
try {
$settings = Setting::all('key', 'value')
->keyBy('key')
->transform(function ($setting) {
return $setting->value;
})
->toArray();
config([
'settings' => $settings
]);
config(['app.name' => config('settings.app_name')]);
} catch (\Exception $e) {
return false;
}
推荐阅读
- ionic-framework - 为什么我会收到 NG0303 或 NG0300 错误?(角度测试,Karma Jasmine)
- python - 我们可以有条件地调用 pytest 夹具吗?
- android - Flutter中如何通过provider包调用一个函数通过语音输入来执行一些功能?
- java - 选择导航菜单项时不会替换上一个片段
- r - 线性混合建模能否处理响应变量和/或预测变量中的 NA(缺失数据)?
- r - 在 R 中带有循环的时钟以重命名带时间戳的文件
- c# - Unity2D:沿路径移动对象 - 不均匀分布的航路点 - 需要恒定速度
- ecmascript-6 - 如何在字符串中不存在时格式化abd get empty time?
- python - 基于半球上点的线密度图
- c# - 异步方法是否有自己的线程