Laravel ParseError 语法错误,意外的标识符"Schema",期望迁移类中出现"function"或"const"



这个问题以前有人问过,但我没有找到一个解决我问题的答案。

我正在使用Laravel 9框架。我有一个类扩展迁移,我编辑我的类如下代码,但在运行迁移命令后,我得到上述错误。

return new  class extends Migration
{
Schema::create('articles', function (Blueprint $table) {
$table->increments('id');
$table->string('title');
$table->string('body');
$table->integer('user_id');
$table->timestamps();
});
}
public function down()
{
Schema::dropIfExists('articles');
}
} ;

必须将Schema::create语句放在名为up的公共函数中

完整的代码如下:

<?php
use IlluminateDatabaseMigrationsMigration;
use IlluminateDatabaseSchemaBlueprint;
use IlluminateSupportFacadesSchema;
return new class extends Migration
{
public function up()
{
Schema::create('articles', function (Blueprint $table) {
$table->increments('id');
$table->string('title');
$table->string('body');
$table->integer('user_id');
$table->timestamps();
});
}
public function down()
{
Schema::dropIfExists('articles');
}
};

相关内容

最新更新