如何为这样的一对一关系建立雄辩关系



我的表结构是这样的:

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);
}

如何设置关系?

在你的情况下,教育有很多农民,农民属于教育。

您应该使用belongsTo()

public function education()
{
    return $this->belongsTo(Educations::class);
}

这行应该是这样的,

 return $this->hasOne('AppyourModel');

例如

 return $this->hasOne('AppEducations');

最新更新