0

When I run php artisan migrate, I am getting an error like this:

In 2017_12_26_045926_create_table_articles.php line 41:

  Parse error: syntax error, unexpected 'public' (T_PUBLIC), expecting ',' or  
   ')'              

This is my articles tables:

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

I am adding foreign key for articles and comments, but the articles tables when migrate is giving errors like above. What's wrong?

1
  • This is not valid PHP. It's even telling you which line is not valid (which the rest of us can't see). Commented Mar 14, 2018 at 4:45

3 Answers 3

1

The error is because you are using the schema class again which is missing the closing tag ")};" and there is no need to use Schema class again you can use the same object to add a foreign key to the table. Try the below code :

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');
}
Sign up to request clarification or add additional context in comments.

Comments

0

You dont need to alter table when you are creating the table to put 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');
});

However, you can do that correctly with this code

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

4 Comments

when i remove alter table ..thie error.. SQLSTATE[HY000]: General error: 1215 Cannot add foreign key constraint (SQL : alter table articles add constraint articles_user_id_foreign foreign key (user_id) references users (id) on delete cascade)
that is because you need to put unsigned I'll modify my answer now
i am put unsigned on my articles tables,, but now the error is.. SQLSTATE[42000]: Syntax error or access violation: 1067 Invalid default val ue for 'is_parent' > i put unsigned also in my coments table..
you must put 1 (true) or 0(false) into a boolean default value i edited again my code. is_parent field is into coments table? do this $table->boolean('is_parent')->default(0);
0

You're missing the closure on 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();
});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.