laravel 数据库迁移出现错误

laravel db migrate getting error

当我 运行 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');
    }
}

我正在为文章和评论添加外键,但是迁移时文章表出现如上所示的错误。怎么了?

您错过了 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();
});

当你创建 table 来放置外国人时,你不需要改变 table。

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');
});

错误是因为您再次使用架构 class,它缺少结束标记 ")};"并且无需再次使用 Schema class,您可以使用相同的对象向 table 添加外键。 试试下面的代码:

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');
}

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');
}