Have I stumbled upon some weird edge-case, a legitimate bug in Laravel, or am I just doing something wrong?
I have a Model that has a relationship called details. The relationship returns a different Model depending on the change_type_id attribute - which is never null and is always >= 1 && <= 6
The problem I am having is when trying to Eager Load the details relationship. Here's the details:
- Laravel v5.7
Change Model
class Change extends Model {
public function details()
{
switch ($this->change_type_id) {
case 1:
return $this->hasOne(LineManagerChange::class);
case 2:
return $this->hasOne(NameChange::class);
case 3:
return $this->hasOne(ContractChange::class);
case 4:
return $this->hasOne(PositionChange::class);
case 5:
return $this->hasOne(CampaignChange::class);
case 6:
return $this->hasOne(StatusChange::class);
}
}
}
Change Resource
class ChangeResource extends JsonResource
{
public function toArray($request)
{
return [
'id' => $this->id,
// the issue occurs even when I don't wrap the resource here
'details' => new ChangeDetailsResource($this->details)),
];
}
}
This works
When I wrap the response in the Resource class without eager loading the relation, like this:
return ChangeResource::collection(Change::limit(10)->get());
It works as expected and has no problem returning the details relation.
This doesn't work
However, when I try to eager load the relationship I'm getting an error:
// Both of these fail
return Change::with('details')->limit(10)->get();
return ChangeResource::collection(Change::with('details')->limit(10)->get());
Call to a member function addEagerConstraints() on null
Does anyone know why this might be the case?
PS. I've had a look into Polymorphic relationships, but I'm not sure if they're suitable for my use-case?