쿼리가 3 개 있습니다. 병합 된 컬렉션을 created_at
으로 정렬해야합니다.Laravel 병합 병합 방법
세 가지 쿼리 빌더 모음을 병합하고 정렬하는 방법은 무엇입니까?
$posts1 =\DB::table('posts')
->select('posts.*', 'users.nom','pratics.titre')
->join('pratics', 'posts.pratic_id','=','pratics.id')
->join('users', 'posts.user_id','=','users.id')
->where('pratics.user_id',$id)
->orderBy('posts.id', 'desc')
->get();
$posts2 =\DB::table('posts')
->select('posts.*', 'users.nom','pratics.titre')
->join('journals', 'posts.pratic_id','=','journals.id')
->join('users', 'posts.user_id','=','users.id')
->where('journals.user_id',$id)
->orderBy('posts.id', 'desc')
->get();
$posts3 =\DB::table('posts')
->select('posts.*', 'users.nom','pratics.titre')
->join('exos', 'posts.exo_id','=','exos.id')
->join('users', 'posts.user_id','=','users.id')
->where('exos.user_id',$id)
->orderBy('posts.id', 'desc')
->get();
$posts = array_merge($posts1,$posts2, $posts3)->sortby('created_at');
'UNION'에 모든 쿼리를 제안하고 PHP에서 이들 3 개의 배열을 병합하지 말 것을 제안합니다. https://laravel.com/docs/5.3/queries#unions –
union for this : $ posts1 = \ DB :: table ('posts') -> select ('posts. *', 'users.nom' ('사용자', 'posts.user_id', '=', '사용자 이름', '사용자 이름', '사용자 이름', '사용자 이름', ' users.id ') -> 여기서 ('pratics.user_id ', $ id); ('posts. *', 'users.nom', 'pratics.titre') -> join ('저널', 'posts.pratic_id') -> = ','journals.id ') -> ('사용자 ','posts.user_id ','= ','users.id ') -> 여기서 ('journals.user_id ', $ id) > 노동 조합 ($ posts1) -> get(); – nabil