Using spatie activity log
I have multiple models that are logging activity, and an ActivityResource
class.
inside the ActivityResource
class im calling other Resource classes based on subject_type
.
$subjectResource = match ($this->subject_type) {
Profile::class => ProfileResource::class,
Invoice::class => InvoiceResource::class,
Payment::class => PaymentResource::class
};
i would like to eager load a deep nested relation from subject
based on the the subject_type
, this is what i’m currently doing.
$profile = match ($this->subject_type) {
Profile::class => $this->whenLoaded('subject'),
Invoice::class => $this->whenLoaded('subject.profile'),
Payment::class => $this->whenLoaded('subject.invoice.profile'),
};
Then i’m doing this
$activities = Activitiy::with([
'subject'=>['invoice.profile','profile']
]);
ActivityResource::collection($activities);
The issue is when trying to eager load the relation i’m getting an error
Call to undefined relationship [profile] on model [App\Models\Profile].
The question is how to conditionaly eager load deep relationships from polymorphic data?