How to Merge Two Eloquent Collections?

后端 未结 6 805
梦如初夏
梦如初夏 2020-11-28 08:30

I have a questions table and a tags table. I want to fetch all questions from tags of a given question. So, for example, I may have the tags \"Travel,\" \"Trains\" and \"Cul

6条回答
  •  再見小時候
    2020-11-28 08:55

    Creating a new base collection for each eloquent collection the merge works for me.

    $foo = collect(Foo::all());
    $bar = collect(Bar::all());
    $merged = $foo->merge($bar);
    

    In this case don't have conflits by its primary keys.

提交回复
热议问题