【问题标题】:My migration file raising an error while running "php artisan migrate"我的迁移文件在运行“php artisan migrate”时引发错误
【发布时间】:2020-05-19 05:29:08
【问题描述】:

我得到的错误是

“Illuminate\Database\QueryException : SQLSTATE[42S22]: 找不到列: 1054 'where 子句'中的未知列 'key' (SQL: select * from admin_settings where key != null limit 1)”

因为app/Providers/AppServiceProvider.php中的函数

 public function boot()
    {
        if (Schema::hasTable('admin_settings')) {
            $google_analytics_details = AdminSetting::where('key','!=','null')->first();

        }else {
            $google_analytics_details = '';
        }        
        View::share('google_analytics_details', $google_analytics_details);
    }

当我注释启动功能的代码时,它会成功迁移。

我正在寻找此视图共享的替代方案。 谁能帮帮我??

我的迁移文件内容:

<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class UpdateAdminSettingsTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        //
         Schema::table('admin_settings', function (Blueprint $table) {
            $table->dropColumn('google_analytics_code');
        });

        Schema::table('admin_settings', function (Blueprint $table) {
            $table->longText('key')->nullable()->after('id');
            $table->longText('value')->nullable()->after('key');

        });

    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        //
         Schema::table('admin_settings', function (Blueprint $table) {

        });


    }
}

【问题讨论】:

  • 您是否在标题中加入了“使用 App/AdminSetting”?
  • 有关键字段吗?
  • 表格中有那一列吗?你能发布你的迁移吗?也许使用View Composers
  • 你的表结构是什么?您是否拼写检查了列的名称? (键与键等)
  • 所以您的表存在但该列不存在?您可以使用if (Schema::hasColumn('admin_settings', 'key')) { // } 检查该列是否存在

标签: php laravel laravel-6 php-7.2


【解决方案1】:

你可以使用view composers doc

如果您想将变量共享到一个视图(可以加载或扩展到其他视图,例如layout.app),您可以指定视图名称,如下例所示

简单示例:

View::composer('view-name', function ($view) {
             $view->with('key', 'value');
        });

或者,如果您在所有视图中都需要它,您可以使用 * 作为视图名称,如下所示

View::composer('*', function ($view) {
             $view->with('key', 'value');
        });

解决问题的另一种方法

您的迁移问题也可以在共享视图之前通过条件解决

 public function boot()
    {
        if ( !app()->runningInConsole() ){
             // your code here
        }
    }

【讨论】:

    猜你喜欢
    • 2017-09-27
    • 2019-02-24
    • 2017-05-31
    • 2018-12-05
    • 2018-11-18
    • 1970-01-01
    • 1970-01-01
    • 2018-08-04
    • 2023-03-13
    相关资源
    最近更新 更多