php Laravel迁移类未找到

dwbf0jvd  于 2023-05-12  发布在  PHP
关注(0)|答案(1)|浏览(133)

运行php artisan migrate迁移数据库时
我得到以下错误:

In Migrator.php line 448:

  Class 'AddNewStatusFlagsToUsersTable' not found

这是我的迁移代码。

<?php

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

class AddNewStatusFlagsToUsersTable extends Migration
{
    public function up()
    {
        Schema::table('users', function (Blueprint $table) {
            $table->boolean('decline_plg')->default(false);
            $table->boolean('unresponsive')->default(false);
            $table->boolean('filing_directly_to_amazon')->default(false);
        });
    }

    public function down()
    {
        Schema::table('users', function (Blueprint $table) {
            $table->dropColumn('decline_plg');
            $table->dropColumn('unresponsive');
            $table->dropColumn('filing_directly_to_amazon');
        });
    }
}

文件名2022_10_05_184432_add_new_status_flags_to_users_table.php
我运行了这个命令:
php artisan clear-compiled
php artisan optimize:clear
composer dump-autoload
php artisan optimize
但仍然没有任何工作。
如何解决Laravel的迁移问题?

js5cn81o

js5cn81o1#

class AddNewStatusFlagsToUsersTable extend Migration更改为return new class extends migration以使用匿名迁移:

<?php
    
    use Illuminate\Database\Migrations\Migration;
    use Illuminate\Database\Schema\Blueprint;
    use Illuminate\Support\Facades\Schema;
    
    return new class extends Migration
    {
        public function up()
        {
            Schema::table('users', function (Blueprint $table) {
                $table->boolean('decline_plg')->default(false);
                $table->boolean('unresponsive')->default(false);
                $table->boolean('filing_directly_to_amazon')->default(false);
            });
        }
    
        public function down()
        {
            Schema::table('users', function (Blueprint $table) {
                $table->dropColumn('decline_plg');
                $table->dropColumn('unresponsive');
                $table->dropColumn('filing_directly_to_amazon');
            });
        }
    }
  • laravel 8可选:https://laravel.com/docs/8.x/migrations#anonymous-migrations
  • Laravel 10中的标准方法:https://laravel.com/docs/10.x/migrations#migration-structure

相关问题