Skip to content

[Question] Schema Builder Foreign Keys #240

Closed
@ghost

Description

I am trying to understand how to add foreign key constraints in the schema builder. Having spent some time reading over https://github.com/laravel/framework/blob/master/src/Illuminate/Database/Schema/Blueprint.php and https://github.com/laravel/framework/blob/master/src/Illuminate/Database/Schema/Grammars/MySqlGrammar.php, I am afraid I still can't understand how to add foreign keys.

The argument to the foreign key can be a string or array which is referenced internally as $columns. Is this the name of the column on the table with the foreign key, or the name of the key on the relational table?

Many thanks!

Schema::create('comments', function($table)
{
    $table->increments('id');
    $table->integer('post_id')->foreign('post_id');
    $table->text('content');
    $table->timestamps();
});

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions