Merge and Sort two Eloquent Collections?

前端 未结 2 753
隐瞒了意图╮
隐瞒了意图╮ 2020-12-17 16:07

I\'ve two Collections and I want merge it to one variable (of course, with ordering by one collumn - created_at). How Can I do that?

My Controllers look

2条回答
  •  被撕碎了的回忆
    2020-12-17 16:32

    A little workaround for your problem.

    $posts = collect(Post::onlyTrashed()->get());
    $comments = collect(Comment::onlyTrashed()->get());
    
    $trash = $posts->merge($comments)->sortByDesc('deleted_at');
    

    This way you can just merge them, even when there are duplicate ID's.

提交回复
热议问题