Laravel 预加载不起作用,而延迟加载同样有效
Laravel Eager Loading not working, while lazy loading the same works
目前我正在加载这样的关系:
$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"]);
}
而且这个在预先加载下工作得很好,所以我不知道到底发生了什么,因为一个有效,但另一个无效。
有人知道出了什么问题吗?提前致谢!
您必须在两个模型(Match
和 PouleTeam
)中使用 Compoships
trait/model:README
目前我正在加载这样的关系:
$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"]);
}
而且这个在预先加载下工作得很好,所以我不知道到底发生了什么,因为一个有效,但另一个无效。
有人知道出了什么问题吗?提前致谢!
您必须在两个模型(Match
和 PouleTeam
)中使用 Compoships
trait/model:README