【问题标题】:Laravel Eager Loading not working, while lazy loading the same worksLaravel Eager Loading 不起作用,而延迟加载同样有效
【发布时间】:2019-01-19 10:27:13
【问题描述】:

目前我正在加载这样的关系:

$matches = App\Match::all();
foreach($matches as $match){
    $match->thuisPouleTeam;
}

我需要加载这些关系的原因是因为它是针对 api 端点的。

它工作正常,但是所有其他关系属性都是通过 ->with() 方法加载的,用于预加载,但是如果我尝试通过预加载来加载这个,它会变成一个错误:

strtolower() expects parameter 1 to be string, array given

我猜这可能是因为我使用compoships package 来允许复合键,所以关系方法如下所示:

public function thuisPouleTeam(){
    return $this->hasOne('App\PouleTeam', ["teamGuid", "pouleGuid"], ["thuisGuid", "pouleGuid"]);
}

但我得到了另一个几乎相同的关系:

public function complementaireMatch()
{
    return $this->hasOne('App\Match', ["thuisGuid", "uitGuid", "pouleGuid"], ["uitGuid", "thuisGuid", "pouleGuid"]);
}

而且这个在预加载时工作得很好,所以我不知道到底发生了什么,因为一个工作,但另一个没有。

有人知道出了什么问题吗?提前致谢!

【问题讨论】:

  • 您是否在两个模型上使用Compoships trait/model?
  • 解决了,谢谢 :) 我错过了文档中的那部分

标签: laravel-5 eloquent eager-loading


【解决方案1】:

您必须在两个模型(MatchPouleTeam)中使用 Compoships 特征/模型:README

【讨论】:

    猜你喜欢
    • 2012-02-04
    • 2013-06-04
    • 1970-01-01
    • 2018-04-27
    • 2020-01-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-01-18
    相关资源
    最近更新 更多