Hello,
First of all, to prevent N+1 queries in my app, I add this in the AppServiceProvider : Model::preventLazyLoading(app()->isLocal()); and removing it does not feel like the good solutions 😄
Here are the key models that creates my issue :
class User extends Model {}
class Team extends Model {}
class Tournament extends Model
{
// DB column : public bool $team_based
public function matchups(): HasMany {
return $this->hasMany(Matchup::class);
}
}
class Matchup extends Model
{
public function tournament(): BelongTo {
return $this->belongsTo(Tournament::class);
}
public function contestants(): MorphToMany {
return $this->morphedByMany($this->tournament->team_based ? Team::class : User::class, 'contestant');
}
}
The issue arises when I want to do $matchup->load('contestants') to render a match for exemple.
I run into the error : Attempt to read property "team_based" on null
Upon debugging, I found out that when dumping $this in the contestants method of my Matchup class has no attribute hydrated so the tournament attribute is null hence the error.
I have obviously check my data, and for this particular matchup, the tournament is definitely set in the database so the issue does not comes from here.
I have read the many to many polymorphic relations documentation and in the exemple, 2 methods are used (I would have to do something like teams() and users() methods but it feels annoying because in my case, it's either one or the other.
Would appreciate any help,
Thanks !