Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

laravel foreign key column naming convention

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
like image 830
alex Avatar asked Dec 18 '22 19:12

alex


1 Answers

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');
}
like image 135
Bogdan Avatar answered Dec 21 '22 07:12

Bogdan