mardi 21 août 2018

Laravel merge excluding the primary object

My question is very basic. I'm aware that it was asked before. But those questions has the answers which used to work in Laravel 4 and I can't quite get it to work in Laravel 5.6.

I'm trying to merge these two:

$posts = Post::where('user_id', $user->user_id)->get();
$comments = Comment::where('user_id', $user->user_id)->get();

The method I tried is this:

$activities = $posts->merge($comments)->sortBy('created_at');

This gives me only $comments In previous questions this seem to work in most cases but not in mine.

Also, I know there is an option to do it like

$activities = array_merge($posts->toArray(), $comments->toArray());

But when I do that I'm no longer able to detect which model or table the object belongs to or can't use the relationships.

I'm open to any solution with that, but I would love the ones where I can still use my relationships.

Laravel 5.6



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire