首页 > 解决方案 > 我无法迁移 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')]);
}

}

标签: phpdatabaselaravelmigrationlaravel-8

解决方案


首先,这不是一个解决方案,每次你刷新数据库并且你必须评论它或者你的队友想要在他的系统上安装项目他不知道你做了什么以及在哪里做了。所以它可能很混乱..所以,当您要迁移时,引导方法会搜索数据库中不存在的设置表,这就是它引发错误的原因。所以你能做什么,你可以把它绑定一个 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;
        }

推荐阅读