Skip to content
Advertisement

My migration file raising an error while running “php artisan migrate”

I am getting the error as

“IlluminateDatabaseQueryException : SQLSTATE[42S22]: Column not found: 1054 Unknown column ‘key’ in ‘where clause’ (SQL: select * from admin_settings where key != null limit 1)”

because of the function in 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);
    }

when i comment the code of the boot function then it migrates successfully.

I am looking an alternative for this view sharing. can anyone help me??

My migration file content:

<?php

use IlluminateDatabaseMigrationsMigration;
use IlluminateDatabaseSchemaBlueprint;
use IlluminateSupportFacadesSchema;

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) {

        });


    }
}

Advertisement

Answer

you can use view composers doc

if you want to share variable to one view (that may be loaded or extended to other views e.g. layout.app) you can specify the view name as example below

simple example:

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

or if you need it in all of your views you can use * as view name like this

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

Another way to solve your problem

your issue with migration can also be solved with a condition before sharing the view

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

User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement