I have a LessonGroup
model and its tabel name is lesson_groups
. if i want to use this table id
in another tabel as a foreign key, what column name should i select that Laravel could distinguish it automatically as a foreign key to lesson_group
table?
lesson_groups
id
name
-----------------------------------------
fields
id
//foreign key to lesson_groups(what should be the name of this column?)
name
Below is an excerpt from the Laravel Eloquent Documentation:
Eloquent determines the default foreign key name by examining the name of the relationship method and suffixing the method name with
_id
.
So for the naming convention to work seamlessly, you would need to have a relation like this defined in your Field
model:
public function lesson_group()
{
return $this->hasOne('App\LessonGroup');
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With