【问题标题】:laravel db migrate getting errorlaravel db 迁移出现错误
【发布时间】:2018-03-14 04:04:58
【问题描述】:

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

我正在为文章和 cmets 添加外键,但迁移时的文章表会出现上述错误。怎么了?

【问题讨论】:

  • 这不是有效的 PHP。它甚至会告诉你哪一行是无效的(我们其他人看不到)。

标签: laravel dbmigrate


【解决方案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');
}

或

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

【讨论】:

    【解决方案2】:

    创建表时不需要更改表来放置外国人。

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

    【讨论】:

    • 当我删除更改表时..thie 错误.. SQLSTATE[HY000]: 一般错误:1215 无法添加外键约束(SQL:更改表articles 添加约束articles_user_id_foreign 外键(@ 987654325@) 在删除级联时引用 users (id)
    • 那是因为你需要把unsigned我现在修改我的答案
    • 我的文章表上没有签名,但现在错误是.. SQLSTATE[42000]: 语法错误或访问冲突:1067 'is_parent' 的默认值无效 > 我也没有签名在我的评论表中..
    • 您必须将 1 (true) 或 0(false) 放入我再次编辑代码的布尔默认值中。 is_parent 字段在 coments 表中?这样做$table->boolean('is_parent')->default(0);
    【解决方案3】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-10-20
      • 1970-01-01
      • 2018-05-02
      • 2020-08-02
      • 2018-05-03
      • 2012-08-01
      • 2019-02-04
      • 1970-01-01
      相关资源
      最近更新 更多