No, you can't do that. Laravel needs to know what to instantiate. Also Eloquent Collection relies on ids, so if you had the same ids for both morphed models, they would override one another.
Thanks jarektkaczyk, I was afraid that might be the case. Is there a better L4 way to handle this particular use-case then?
It depends on what you are trying to achieve.
For example you could convert to and merge base collections in order to work on all of the items:
$certificate = Certificate::find($id);
$materials = $certificate->materials->toBase();
$employees = $certificate->employees->toBase();
$certified = $materials->merge($employees);
// then you can do whatever you need:
$certified->sortBy('created_at');
$latest = $certified->filter(function ($item) {
return $item->created_at > $someDate;
});
$groupped = $certified->groupBy('SOME_COMMON_PROPERTY');
// and so on..
Sign in to participate in this thread!
The Laravel portal for problem solving, knowledge sharing and community building.