Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

risalah's avatar

php artisan migrate gives error

In terminal, when I try to migrate using php artisan migrate, I get this sql error:

   Illuminate\Database\QueryException 

  SQLSTATE[42S02]: Base table or view not found: 1146 Table 'db_sample.settings' doesn't exist (Connection: mysql, SQL: select * from `settings` where `key` = github_client_id limit 1)

  at vendor/laravel/framework/src/Illuminate/Database/Connection.php:793
    789▕         // If an exception occurs when attempting to run a query, we'll format the error
    790▕         // message to include the bindings with SQL, which will make this exception a
    791▕         // lot more helpful to the developer instead of just the database's errors.
    792▕         catch (Exception $e) {
  ➜ 793▕             throw new QueryException(
    794▕                 $this->getName(), $query, $this->prepareBindings($bindings), $e
    795▕             );
    796▕         }
    797▕     }

  i   A table was not found: You might have forgotten to run your database migrations. 
      https://laravel.com/docs/master/migrations#running-migrations

  1   [internal]:0
      Illuminate\Foundation\Application::Illuminate\Foundation\{closure}()
      +12 vendor frames 

  14  app/Repositories/Setting/SettingRepositoryImplement.php:26
      Illuminate\Database\Eloquent\Builder::first()

I believe that I've setup my .env file correctly:

APP_NAME=Laravel
APP_ENV=local
APP_KEY=base64:pUEmW+LfwGvK+n1SD29CZv6iso71gRhKB1ILaYn+vTs=
APP_DEBUG=true
APP_URL=http://localhost

LOG_CHANNEL=stack
LOG_DEPRECATIONS_CHANNEL=null
LOG_LEVEL=debug

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=db_sample
DB_USERNAME=root
DB_PASSWORD=unknown

Has anyone ever had a problem like this, and how do I fix it?

0 likes
15 replies
risalah's avatar

@dacfabre inside like this:

<?php

namespace App\Repositories\Setting;

use App\Models\Setting;
use Illuminate\Support\Facades\Cache;
use LaravelEasyRepository\Implementations\Eloquent;

class SettingRepositoryImplement extends Eloquent implements SettingRepository
{
    /**
     * @property Model|mixed $model;
     */
    protected $model;

    public function __construct(Setting $model)
    {
        $this->model = $model;
    }

    public function getValueByKey(string $key): ?string
    {
        $value = Cache::remember("settings.$key", now()->addDay(), function () use ($key) {
            $setting = $this->model->where('key', $key)->first();
            return $setting ? $setting->value : null;
        });

        return $value;
    }

    public function save(array $data): ?Setting
    {
        $setting = parent::save($data);

        if ($setting) {
            Cache::forget("settings.{$setting->key}");
        }

        return $setting;
    }

    public function update($id, array $data): ?Setting
    {
        $setting = parent::update($id, $data);

        if ($setting) {
            Cache::forget("settings.{$setting->key}");
        }

        return $setting;
    }
}

dacfabre's avatar

@risalah this is where your error is

public function __construct(Setting $model)
    {
        $this->model = $model;
    }

your Setting model dont have a corresponding table settings this class is probably being run on providers or somewhere that boots laravel.

try to comment this line $setting = $this->model->where('key', $key)->first();

create your settings migration and then run your migration

risalah's avatar

@dacfabre do you mean like this?

public function getValueByKey(string $key): ?string
    {
        $value = Cache::remember("settings.$key", now()->addDay(), function () use ($key) {
            // $setting = $this->model->where('key', $key)->first();
            return $setting ? $setting->value : null;
        });

        return $value;
    }

2023_03_05_123711_create_settings_table.php

<?php

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

return new class extends Migration
{
    /**
     * Run the migrations.
     */
    public function up(): void
    {
        Schema::create('settings', function (Blueprint $table) {
            $table->id();
            $table->string('key')->unique();
            $table->longtext('value')->nullable();
            $table->timestamps();
        });
    }

    /**
     * Reverse the migrations.
     */
    public function down(): void
    {
        Schema::dropIfExists('settings');
    }
};

Model Setting.php

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Factories\HasFactory;

class Setting extends Model
{
    use HasFactory;

    protected $fillable = ['key', 'value'];

    public static function getValueByKey($key)
    {
        $setting = self::where('key', $key)->first();

        return $setting ? $setting->value : null;
    }
}

then I tried to run php artisan migrate again, the result is:

Undefined variable $setting

  at app/Repositories/Setting/SettingRepositoryImplement.php:27
     23▕     public function getValueByKey(string $key): ?string
     24▕     {
     25▕         $value = Cache::remember("settings.$key", now()->addDay(), function () use ($key) {
     26▕             // $setting = $this->model->where('key', $key)->first();
  ➜  27▕             return $setting ? $setting->value : null;
     28▕         });
     29▕ 
     30▕         return $value;
     31▕     }

  1   app/Repositories/Setting/SettingRepositoryImplement.php:27
      Illuminate\Foundation\Bootstrap\HandleExceptions::Illuminate\Foundation\Bootstrap\{closure}()
      +3 vendor frames 

  5   app/Repositories/Setting/SettingRepositoryImplement.php:28
      Illuminate\Support\Facades\Facade::__callStatic()
dacfabre's avatar

@risalah well of course you will have to comment return $settings as well, or return null; as your $settings now is undefined

risalah's avatar

@dacfabre my error is now like this:

SQLSTATE[42S02]: Base table or view not found: 1146 Table 'db_sample.settings' doesn't exist (Connection: mysql, SQL: select `value` from `settings` where `key` = verify_account_email limit 1)

  at vendor/laravel/framework/src/Illuminate/Database/Connection.php:793
    789▕         // If an exception occurs when attempting to run a query, we'll format the error
    790▕         // message to include the bindings with SQL, which will make this exception a
    791▕         // lot more helpful to the developer instead of just the database's errors.
    792▕         catch (Exception $e) {
  ➜ 793▕             throw new QueryException(
    794▕                 $this->getName(), $query, $this->prepareBindings($bindings), $e
    795▕             );
    796▕         }
    797▕     }

  i   A table was not found: You might have forgotten to run your database migrations. 
      https://laravel.com/docs/master/migrations#running-migrations

  1   [internal]:0
      Illuminate\Foundation\Application::Illuminate\Foundation\{closure}()
      +13 vendor frames 

  15  routes/web.php:36
      Illuminate\Database\Eloquent\Builder::value()
dacfabre's avatar

@risalah whats in your web.php line 36? i assume its also related to Setting model, comment all your code that is related to this model. then migrate your db

dacfabre's avatar

@risalah i quick workaround i would suggest is, create a fresh laravel project. and do your setting migration there

risalah's avatar

@dacfabre yeah when I commented out line 36, and it worked again. can you explain my problem? because before it was fine, this error occurs when I create a new migration page

dacfabre's avatar

@risalah unfortunately, i cant pinpoint your problem unless i see your codebase. please accept the answer if your problem is already solved. thanks

newbie360's avatar

@risalah If you know where is the starting point, you can wrap the trigger code by

if (! app()->runningInConsole()) {
    ...
}
newbie360's avatar
Level 24

@risalah

if (! app()->runningInConsole()) {
    // in your case, when you run `php artisan migrate`

    // all the code here won't fired
}
1 like

Please or to participate in this conversation.