我的表结构是这样的:
farmers
id
name
education_id (used to capture max education)
educations (names of the education levels like BS / MS / PhD etc.)
id
name
我尝试以下代码失败-
In farmers model (app/fermers.php) I added this code:
public function education()
{
return $this->hasOne(Educations::class);
}
如何建立关系?
答案 0 :(得分:2)
在您的情况下,教育有很多农民,农民属于教育。
您应该使用belongsTo()
:
public function education()
{
return $this->belongsTo(Educations::class);
}
答案 1 :(得分:0)
该行应该像
return $this->hasOne('App\yourModel');
例如,
return $this->hasOne('App\Educations');