Support the ongoing development of →
Database Eloquent
Last updated 4 months ago.

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.

Last updated 4 months ago.

Thanks jarektkaczyk, I was afraid that might be the case. Is there a better L4 way to handle this particular use-case then?

Last updated 4 months ago.

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:

$latest = $certified->filter(function ($item) {
  return $item->created_at > $someDate; 

$groupped = $certified->groupBy('SOME_COMMON_PROPERTY');

// and so on..
Last updated 4 months ago.

Sign in to participate in this thread!


Your banner here too?

Robarelli robarelli Joined 14 Feb 2014


We'd like to thank these amazing companies for supporting us

Your logo here?

The Laravel portal for problem solving, knowledge sharing and community building.

© 2022 - All rights reserved.