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

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.
0

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.
0

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..
Last updated 4 months ago.
0

Sign in to participate in this thread!

LoadForge

Your banner here too?

Robarelli robarelli Joined 14 Feb 2014

Moderators

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 Laravel.io - All rights reserved.