Laravel 5.4: Error creating table - Unexpected '(', expecting Identifier (T_STRING) or variable (T_VARIABLE) or '{' or '$'

Asked

Viewed 324 times

0

When executing the command php Artisan migrate to create the table in the database, the error appears below:

[Symfony\Component\Debug\Exception\FatalThrowableError]
  Parse error: syntax error, unexpected '(', expecting identifier (T_STRING) or variable (T_VARIABLE) or '{' or '$'

It seems to be a syntax error, but I can not see where the error is, I have created other tables, including those that are foreign keys of this without any problem.

My Migration is like this:

use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreateConvServsTable extends Migration
{
    public function up()
    {
        Schema::create('conv_servs', function (Blueprint $table) {
            $table->increments('id');
            $table->integer('id_convenio')->unsigned();
            $table->integer('id_especialidade')->unsigned();
            $table->foreign('id_convenio')->references('id')->('convenios')->onDelete('cascade');
            $table->foreign('id_especialidade')->references('id')->('especialidades')->onDelete('cascade');
        });
    }

    public function down()
    {
        Schema::dropIfExists('conv_servs');
    }
}

2 answers

1


This feels wrong:

$table->foreign('id_convenio')->references('id')->('convenios')->onDelete('cascade');
$table->foreign('id_especialidade')->references('id')->('especialidades')->onDelete('cascade');

It doesn’t make much sense ->('convenios')-> and ->('especialidades')->, I’m sure they should be functions.

  • According to Laravel’s documentation, that’s where the name of the table goes. I have created other tables with foreign keys and worked normally.

  • Man, I can’t believe I forgot the ON before the table name. My old God. What a stupid mistake.

  • @Diegovieira if it is programming believe we will always make stupid mistakes :) ... is like me with Portuguese, is a gaffe behind the other XD, can mark the answer as accepted?

1

I forgot to put the "ON" before the table name. Follow the tidy code:

Schema::create('conv_servs', function (Blueprint $table) {
            $table->increments('id');
            $table->integer('id_convenio')->unsigned();
            $table->integer('id_especialidade')->unsigned();
            $table->foreign('id_convenio')->references('id')->on('convenios')->onDelete('cascade');
            $table->foreign('id_especialidade')->references('id')->on('especialidades')->onDelete('cascade');
        });

Browser other questions tagged

You are not signed in. Login or sign up in order to post.