title: Laravel-How to merge two collections when one column is common in both collection?
body: <p>I am using Laravel 5.6.38.</p>
  <p>I have two collections.</p>
  <p><strong>Collection 1</strong></p>
  <pre><code>[{
  &quot;link&quot;: &quot;http://example.com/posts/1&quot;,
  &quot;created_at&quot;: &quot;2018-09-20 05:14:10&quot;,
  &quot;description&quot;: &quot;some desc 1&quot;,
  &quot;post_id&quot;: 1,
  &quot;priority&quot;: &quot;2&quot;,
  &quot;identifiedBy&quot;: &quot;x&quot;,
  
  },
  {
  &quot;link&quot;: &quot;http://example.com/posts/2&quot;,
  &quot;created_at&quot;: &quot;2018-09-20 05:14:10&quot;,
  &quot;description&quot;: &quot;some desc 2&quot;,
  &quot;post_id&quot;: 2,
  &quot;priority&quot;: &quot;3&quot;,
  &quot;identifiedBy&quot;: &quot;x&quot;,
  }]
  </code></pre>
  <p><strong>Collection 2</strong></p>
  <pre><code>[{
  &quot;post_id&quot;: 1,
  &quot;qatagger&quot;: &quot;Mr. X&quot;
  }]
  </code></pre>
  <p><strong>Expected result</strong></p>
  <pre><code>[{
  &quot;link&quot;: &quot;http://example.com/posts/1&quot;,
  &quot;created_at&quot;: &quot;2018-09-20 05:14:10&quot;,
  &quot;description&quot;: &quot;some desc 1&quot;,
  &quot;post_id&quot;: 1,
  &quot;priority&quot;: &quot;2&quot;,
  &quot;identifiedBy&quot;: &quot;x&quot;,
  &quot;qatagger&quot;: &quot;Mr. X&quot;
  },
  {
  &quot;link&quot;: &quot;http://example.com/posts/2&quot;,
  &quot;created_at&quot;: &quot;2018-09-20 05:14:10&quot;,
  &quot;description&quot;: &quot;some desc 2&quot;,
  &quot;post_id&quot;: 2,
  &quot;priority&quot;: &quot;3&quot;,
  &quot;identifiedBy&quot;: &quot;x&quot;,
  }]
  </code></pre>
  <p>I tried $collection1-&gt;merge($collection2), gets result</p>
  <pre><code>[{
  &quot;link&quot;: &quot;http://example.com/posts/1&quot;,
  &quot;created_at&quot;: &quot;2018-09-20 05:14:10&quot;,
  &quot;description&quot;: &quot;some desc 1&quot;,
  &quot;post_id&quot;: 1,
  &quot;priority&quot;: &quot;2&quot;,
  &quot;identifiedBy&quot;: &quot;superadmin&quot;
  },
  {
  &quot;link&quot;: &quot;http://example.com/posts/2&quot;,
  &quot;created_at&quot;: &quot;2018-09-20 05:14:10&quot;,
  &quot;description&quot;: &quot;some desc 2&quot;,
  &quot;post_id&quot;: 2,
  &quot;priority&quot;: &quot;3&quot;,
  &quot;identifiedBy&quot;: &quot;superadmin&quot;
  },
  {
  &quot;post_id&quot;: 1,
  &quot;qatagger&quot;: &quot;Mr. x&quot;
  }]
  </code></pre>
  <p>Is there anyway I can get the expected result with out using any loop?</p>
  <p><strong>Update</strong>
  <em>1st join</em></p>
  <pre><code>$posts = DB::table('posts')
              -&gt;where('posts.post_status_id', '=', $status)
              -&gt;leftJoin(DB::raw(&quot;(SELECT post_id, comment, comment_type FROM comments WHERE comment_id in (SELECT MAX(comment_id) FROM comments
      GROUP BY comment_type, post_id ASC) AND comment_type = 'vip_comment') comments_vip&quot;), 'posts.post_id', '=', 'comments_vip.post_id')
              -&gt;leftJoin(DB::raw(&quot;(SELECT post_id, comment, comment_type FROM comments WHERE comment_id in (SELECT MAX(comment_id) FROM comments
      GROUP BY comment_type, post_id ASC) AND comment_type = 'pm_comment') comments_pm&quot;), 'posts.post_id', '=', 'comments_pm.post_id')
              -&gt;leftJoin(DB::raw(&quot;(SELECT post_id, comment, comment_type FROM comments WHERE comment_id in (SELECT MAX(comment_id) FROM comments GROUP BY comment_type, post_id ASC) AND comment_type = 'bagging_qa_comment') comments_bq&quot;),'posts.post_id', '=', 'comments_bq.post_id')
              -&gt;leftJoin('users AS identifiedByUser', function($join) {
                  $join-&gt;on('posts.post_identified_by', '=', 'identifiedByUser.id');
              })
              -&gt;select(['posts.post_id as post_id', 'posts.post_link as link', 'posts.post_status_id as status_id', 'posts.post_description as description', 'posts.post_priority as priority', 'posts.created_at as created_at', 'comments_vip.comment AS vip_comment', 'comments_pm.comment AS 
              pm_comment', 'comments_bq.comment as bagger_qa_comment', 'identifiedByUser.name as identifiedBy'])
              -&gt;groupBy('post_id')
              -&gt;get();
  </code></pre>
  <p><em>2nd join</em></p>
  <pre><code>$baggers = DB::table('post_baggings')
                      -&gt;leftJoin('posts', 'post_baggings.post_id', '=', 'posts.post_id' )
                      -&gt;join('users AS baggers', function($join) {
                          $join-&gt;on('post_baggings.bagging_team_id', '=', 'baggers.id');
                      })-&gt;select(array('posts.post_id as post_id', 'baggers.name as bagger'))
                      -&gt;get()-&gt;keyBy('post_id'); 
  </code></pre>
  <p><em>Merging</em></p>
  <pre><code>$out = [];
      foreach ($posts as $key =&gt; $post){
          $post-&gt;priority = Priority::where('priority_id', '=', $post-&gt;priority)-&gt;pluck('display_name')-&gt;first();
          $post = new Collection($post);
          $out[] = $post-&gt;merge($baggers[$post['post_id']]);
      }
  </code></pre>