执行php artisan migrate报错:Call to undefined method Illuminate\Support\Fluent::after()
解决Laravel迁移中
Call to undefined method Illuminate\Support\Fluent::after()错误 错误原因
该错误是由于你使用的Laravel版本中,unsignedInteger()方法返回的是Illuminate\Support\Fluent实例,而这个实例没有after()方法。在旧版本Laravel(如5.8及更早)中,unsignedInteger并不支持链式调用after,需要改用integer()配合unsigned()的写法。
修复方案
修改迁移文件中的字段定义代码,将:
$table->unsignedInteger('cod_esocial')->after('cod');
替换为:
$table->integer('cod_esocial')->unsigned()->after('cod');
修改后的完整迁移文件如下:
<?php use Illuminate\Database\Schema\Blueprint; use Illuminate\Database\Migrations\Migration; class AlterTableRhRacaCorAddCodEsocial extends Migration { /** * Run the migrations. * * @return void */ public function up() { Schema::table('rh_raca_cor', function(Blueprint $table){ $table->integer('cod_esocial')->unsigned()->after('cod'); }); DB::statement("update rh_raca_cor set cod_esocial = case when id = 1 then 4 when id = 2 then 1 when id = 3 then 5 when id = 4 then 3 when id = 5 then 2 else 6 end; "); } /** * Reverse the migrations. * * @return void */ public function down() { Schema::table('rh_raca_cor', function(Blueprint $table){ $table->dropColumn('cod_esocial'); }); } }
验证修复
保存修改后的迁移文件,重新执行:
php artisan migrate
即可正常运行迁移。
内容的提问来源于stack exchange,提问作者Bruno Silva
相关产品推荐
相关产品推荐

