Search code examples
laravel-5eloquenteager-loading

Laravel Eager Loading not working, while lazy loading the same works


Currently I'm loading a relation like this:

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

The reason being i need those relations loaded because it is for an api endpoint.

It works fine, but all other relation attributes are loaded via the ->with() method for eager loading, but if I try to load this one via eager loading, it turns into an error:

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

I'm guessing this is maybe because I use the compoships package to allow for composite keys, so the relation method looks like this:

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

But i got another relation which is practically the same:

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

And this one works fine with eager loading, so I don't know what is happening really, since one works, but the other one doesn't.

Anyone has any idea what is going wrong? Thanks in advance!


Solution

  • You have to use the Compoships trait/model in both models (Match and PouleTeam): README