Laravel 5.7-渴望加载关系的关系

时间:2019-01-25 21:23:14

标签: laravel laravel-5 eloquent laravel-5.7

该如何处理?我尝试过:

return $this->hasMany(ResolutionLog::class, 'ant_id', 'id')->with('resolutionStatus');

并显示错误:

  

调用模型[App \ Models \ ResolutionLog]上未定义的关系[resolutionLog]。

或者在修补程序中,我在获取resolutionLog集合并尝试访问resolutionStatus关系时遇到此错误:

  

带有消息“ App / Models / ResolutionLog :: resolutionStatus必须返回关系实例”的LogicException。

这是resolutionStatus模型上的ResolutionLog关系:

ResolutionLog.php

public function resolutionStatus()
{
    $this->hasOne(ResolutionStatus::class, 'id', 'resolution_status_id');
}

以及resolutionLog模型上的ResolutionStatus关系:

ResolutionStatus.php

public function resolutionLog()
{
    return $this->hasMany(ResolutionLog::class, 'resolution_status_id', 'id');
}

ResolutionLog和一个ResolutionStatus之间应该是一个多对多的关系。一个ResolutionStatus可以有多个ResolutionLog

1 个答案:

答案 0 :(得分:4)

您应该像这样返回关系:

public function resolutionStatus()
{
    // you forgot to return it
    return $this->hasOne(ResolutionStatus::class, 'id', 'resolution_status_id');
}