I've been trying to implement this on both User side and Post side, both it doesn't seem like Eloquent can handle that"
What implementation have you tried? What didn't work?
Hey artisans,
I've been scratching my head every time I try to do this. I have 2 types of user, say UserA and UserB. And 2 types of posts: PostA and PostB. I want to create a table, let's say items, that's going to morph both:
$table->morphs('user');
$table->morphs('post');
I've been trying to implement this on both User side and Post side, both it doesn't seem like Eloquent can handle that, so if everyone has the idea how to do it, it would be nice.
And bonus question: if this is not possible, my backup plan is to create a table userA_items and userB_items and use morphs there:
$table->unsignedInteger('user_id');
$table->morphs('post');
Is there a way to grab all user's items regardless of their type. Meaning: $user->itemsA would be morphedByMany(PostA::class), and same for $user->itemsB. I want to get $user->items that would automatically morph the Post type (return an instance of the model). Yeah I could simply do hasMany on the User model but that would not morph the post. :)
Update: if getting all items is not possible with Eloquent relationships, it would be nice if someone knows a way to combine itemsA(), itemsB() queries to get items. I can do collection merge, but I need pagination and opting for less queries :-)
Thanks!
Please or to participate in this conversation.