当我运行php artisan migrate
时,我收到如下错误:
In 2017_12_26_045926_create_table_articles.php line 41:
Parse error: syntax error, unexpected 'public' (T_PUBLIC), expecting ',' or
')'
这是我的文章表格:
public function up()
{
Schema::create('articles', function (Blueprint $table) {
$table->increments('id');
$table->integer('user_id');
$table->integer('category_id');
$table->string('title');
$table->text('content');
$table->boolean('is_show')->default(false);
$table->boolean('is_active')->default(false);
$table->integer('page_showing')->default(0);
$table->string('header_pic');
$table->softDeletes();
$table->timestamps();
Schema::table('articles', function($table){
$table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
$table->foreign('category_id')->references('id')->on('categories')->onDelete('cascade');
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('articles');
}
}
我正在为文章和评论添加外键,但迁移时的文章表会出现上述错误。怎么了?
答案 0 :(得分:1)
错误是因为您再次使用了缺少结束标记的模式类“)};”并且不需要再次使用Schema类,您可以使用相同的对象向表中添加外键。 请尝试以下代码:
public function up()
{
Schema::create('articles', function (Blueprint $table) {
$table->increments('id');
$table->integer('user_id');
$table->integer('category_id');
$table->string('title');
$table->text('content');
$table->boolean('is_show')->default(false);
$table->boolean('is_active')->default(false);
$table->integer('page_showing')->default(0);
$table->string('header_pic');
$table->softDeletes();
$table->timestamps();
$table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
$table->foreign('category_id')->references('id')->on('categories')->onDelete('cascade');
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('articles');
}
OR
public function up()
{
Schema::create('articles', function (Blueprint $table) {
$table->increments('id');
$table->integer('user_id');
$table->integer('category_id');
$table->string('title');
$table->text('content');
$table->boolean('is_show')->default(false);
$table->boolean('is_active')->default(false);
$table->integer('page_showing')->default(0);
$table->string('header_pic');
$table->softDeletes();
$table->timestamps();
Schema::table('articles', function($table){
$table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
$table->foreign('category_id')->references('id')->on('categories')->onDelete('cascade');
}); //closing Schema class tag
}); //closing Schema class tag
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('articles');
}
答案 1 :(得分:0)
您错过了Schema::create
Schema::create('articles', function (Blueprint $table) {
$table->increments('id');
$table->integer('user_id');
$table->integer('category_id');
$table->string('title');
$table->text('content');
$table->boolean('is_show')->default(false);
$table->boolean('is_active')->default(false);
$table->integer('page_showing')->default(0);
$table->string('header_pic');
$table->softDeletes();
$table->timestamps();
});
答案 2 :(得分:0)
在创建要放置foreigns的表时,您不需要更改表。
Schema::create('articles', function (Blueprint $table) {
$table->increments('id');
$table->integer('user_id')->unsigned(); // Or $table->unsignedInteger('user_id');
$table->integer('category_id')->unsigned();
$table->string('title');
$table->text('content');
$table->boolean('is_show')->default(0);
$table->boolean('is_active')->default(0);
$table->integer('page_showing')->default(0);
$table->string('header_pic');
$table->softDeletes();
$table->timestamps();
$table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
$table->foreign('category_id')->references('id')->on('categories')->onDelete('cascade');
});
但是,您可以使用此代码正确执行此操作
Schema::create('articles', function (Blueprint $table) {
$table->increments('id');
$table->integer('user_id')->unsigned();
$table->integer('category_id')->unsigned();
$table->string('title');
$table->text('content');
$table->boolean('is_show')->default(0);
$table->boolean('is_active')->default(0);
$table->integer('page_showing')->default(0);
$table->string('header_pic');
$table->softDeletes();
$table->timestamps();
});
Schema::table('articles', function($table){
$table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
$table->foreign('category_id')->references('id')->on('categories')->onDelete('cascade');
});