Migration Foreign Key Vs Eloquent Relationships in Laravel

后端 未结 2 1568
情深已故
情深已故 2021-01-30 21:47

In Laravel 5.1 I can see that table column relationships can be set-up in 2 ways:

1) Defining Foreign Keys in the Migration table.

2) De

2条回答
  •  没有蜡笔的小新
    2021-01-30 22:26

    Eloquent assumes the foreign key of the relationship based on the model name. In this case, the App model is automatically assumed to have an app_id foreign key, so in your migrations you do not need to specify:

    $table->foreign('app_id')
    ->references('id')
    ->on('apps')
    ->onDelete('cascade');
    

    Documentation

提交回复
热议问题