Define name for a Foreign Key created from a Migration in Laravel

1

I am building an application using Laravel 5.1 and in my ER Diagram it is very deep and detailed (even with the nomenclature of indexes). I need to know how I can do to create a Foreign Key using the laravel migration methods in which I can define the name of the foreign language, maybe something like:

Schema::create('historico', function (Blueprint $table) {
    $table->increments('id');
    $table->dateTime('data_reg')
          ->default(DB::raw('CURRENT_TIMESTAMP'));
    $table->integer('usuario_id');
    $table->foreign('usuario_id')
          ->foreignName('historico$usuario_id') // <-- procuro isso aqui
          ->references('id')
          ->on('usuario')
          ->onDelete('CASCADE')
          ->onUpdate('CASCADE');
    $table->string('acao', 500);
    $table->longText('conteudo')->nullable();
});
    
asked by anonymous 29.12.2015 / 16:49

1 answer

2

Pass a second parameter in the foreign () function. Example:

$table->foreign('usuario_id', 'nome_da_foreign_key') // o que você procura
          ->references('id')
          ->on('usuario')
          ->onDelete('CASCADE')
          ->onUpdate('CASCADE');

You can see the prototype of the foreign key function in the laravel file Blueprint.php:

/**
     * Specify a foreign key for the table.
     *
     * @param  string|array  $columns
     * @param  string  $name
     * @return \Illuminate\Support\Fluent
     */
    public function foreign($columns, $name = null)
    {
        return $this->indexCommand('foreign', $columns, $name);
    }
    
26.02.2016 / 01:20