ale*_*lex 2 model foreign-keys laravel
我有一个LessonGroup模型,其表格是lesson_groups.如果我想使用此表id在另一个TABEL作为外键,我应该选择什么样的列名Laravel可以自动识别它作为一个外键到lesson_group表?
lesson_groups
id
name
-----------------------------------------
fields
id
//foreign key to lesson_groups(what should be the name of this column?)
name
Run Code Online (Sandbox Code Playgroud)
以下是Laravel Eloquent文档的摘录:
Eloquent通过检查关系方法的名称并使用方法名称后缀来确定默认外键名称
_id.
因此,要使命名约定无缝地工作,您需要在Field模型中定义这样的关系:
public function lesson_group()
{
return $this->hasOne('App\LessonGroup');
}
Run Code Online (Sandbox Code Playgroud)