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

0

When you run the php artisan migrate command to create the table in the database, the following error appears:

[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 already created other tables, including those that are foreign keys of this one with no problem at all.

My migration looks 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');
    }
}
    
asked by anonymous 05.07.2017 / 15:21

2 answers

1

This looks wrong:

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

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

    
05.07.2017 / 15:30
1

I forgot to put the "ON" before the table name. Here is the neat 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');
        });
    
05.07.2017 / 15:41