Я использую Laravel 5.6.38.
У меня есть две коллекции.
Коллекция 1
[{
"link": "http://example.com/videos/1",
"created_at": "2018-09-20 05:14:10",
"description": "some desc 1",
"video_id": 1,
"priority": "2",
"identifiedBy": "x",
},
{
"link": "http://example.com/videos/2",
"created_at": "2018-09-20 05:14:10",
"description": "some desc 2",
"video_id": 2,
"priority": "3",
"identifiedBy": "x",
}]
Коллекция 2
[{
"video_id": 1,
"qatagger": "Mr. X"
}]
Ожидаемый результат
[{
"link": "http://example.com/videos/1",
"created_at": "2018-09-20 05:14:10",
"description": "some desc 1",
"video_id": 1,
"priority": "2",
"identifiedBy": "x",
"qatagger": "Mr. X"
},
{
"link": "http://example.com/videos/2",
"created_at": "2018-09-20 05:14:10",
"description": "some desc 2",
"video_id": 2,
"priority": "3",
"identifiedBy": "x",
}]
Я пробовал $ collection1-> merge ($ collection2), получаю результат
[{
"link": "http://example.com/videos/1",
"created_at": "2018-09-20 05:14:10",
"description": "some desc 1",
"video_id": 1,
"priority": "2",
"identifiedBy": "superadmin"
},
{
"link": "http://example.com/videos/2",
"created_at": "2018-09-20 05:14:10",
"description": "some desc 2",
"video_id": 2,
"priority": "3",
"identifiedBy": "superadmin"
},
{
"video_id": 1,
"qatagger": "Mr. x"
}]
Могу ли я в любом случае получить ожидаемый результат, используя любой цикл?
Обновление 1-е объединение
$videos = DB::table('videos')
->where('videos.video_status_id', '=', $status)
->leftJoin(DB::raw("(SELECT video_id, comment, comment_type FROM comments WHERE comment_id in (SELECT MAX(comment_id) FROM comments
GROUP BY comment_type, video_id ASC) AND comment_type = 'vip_comment') comments_vip"), 'videos.video_id', '=', 'comments_vip.video_id')
->leftJoin(DB::raw("(SELECT video_id, comment, comment_type FROM comments WHERE comment_id in (SELECT MAX(comment_id) FROM comments
GROUP BY comment_type, video_id ASC) AND comment_type = 'pm_comment') comments_pm"), 'videos.video_id', '=', 'comments_pm.video_id')
->leftJoin(DB::raw("(SELECT video_id, comment, comment_type FROM comments WHERE comment_id in (SELECT MAX(comment_id) FROM comments GROUP BY comment_type, video_id ASC) AND comment_type = 'tagging_qa_comment') comments_tq"),'videos.video_id', '=', 'comments_tq.video_id')
->leftJoin('users AS identifiedByUser', function($join) {
$join->on('videos.video_identified_by', '=', 'identifiedByUser.id');
})
->select(['videos.video_id as video_id', 'videos.video_link as link', 'videos.video_status_id as status_id', 'videos.video_description as description', 'videos.video_priority as priority', 'videos.created_at as created_at', 'comments_vip.comment AS vip_comment', 'comments_pm.comment AS
pm_comment', 'comments_tq.comment as tagger_qa_comment', 'identifiedByUser.name as identifiedBy'])
->groupBy('video_id')
->get();
2-е соединение
$taggers = DB::table('video_taggings')
->leftJoin('videos', 'video_taggings.video_id', '=', 'videos.video_id' )
->join('users AS taggers', function($join) {
$join->on('video_taggings.tagging_team_id', '=', 'taggers.id');
})->select(array('videos.video_id as video_id', 'taggers.name as tagger'))
->get()->keyBy('video_id');
Слияние
$out = [];
foreach ($videos as $key => $video){
$video->priority = Priority::where('priority_id', '=', $video->priority)->pluck('display_name')->first();
$video = new Collection($video);
$out[] = $video->merge($taggers[$video['video_id']]);
}