2016-11-26 15 views
1

2 개의 객체가 있습니다. 첫 번째 객체는 객체 작업의 주석을 검색하고 두 번째 객체는 객체 성취도의 주석을 검색합니다. 주석 목록이 들어있는 단일 테이블에 결과를 표시하려고합니다. 보기에서laravel 하나의 배열에있는 두 객체의 결과

$posts1 =\DB::table('posts') 
->select('posts.*', 'profils.nom_profil', 'works.titre', 'profils.imagelogo', 'works.description', 'works.like', 'works.nlike') 
->join('works', 'posts.work_id','=','works.id') 
->join('profils', 'posts.profil_id','=','profils.id') 
->where('works.profil_id',$id) 
->orderBy('posts.id', 'desc') 
->paginate(10); 

$posts2 =\DB::table('posts') 
->select('posts.*', 'profils.nom_profil', 'achievs.titre', 'profils.imagelogo', 'achievs.description', 'achievs.like', 'achievs.nlike') 
->join('achievs', 'posts.achiev_id','=','achievs.id') 
->join('profils', 'posts.profil_id','=','profils.id') 
->where('achievs.profil_id',$id) 
->orderBy('posts.id', 'desc') 
->paginate(10); 

:

@foreach($posts as $post) 
. 
. 
. 
@endforeach 
+0

할 - 먼저 그들에게 컬렉션을 만들> 수() 모두 쿼리를, 당신은 병합 할 수 있습니다. – phobia82

답변

0

Laravel 5.3을 사용하지 않는 경우 get을 사용하면 @iCode에서 언급 한 결과 배열이 반환되므로 원시 array_merge를 사용할 수 있습니다.

$posts = array_merge($posts1->get(),$posts2->get()); 

또는 쿼리에 GET() 메소드를 추가하고 당신이 추가해야

$posts = array_merge($posts1,$posts2); 
1

이 많은 sloution이있을 수 있지만 쉬운 일이 같은 객체 merge이다 :

$posts = $posts1->merge(posts2); 

업데이트

시도 다음과 같이 병합 :

$posts= $posts1->merge($posts2->all()) 
+0

이 오류가 발생했습니다. @foreach ($ posts as $ post){{($ post-> visit == '0')}} : – nabil

+0

에 객체가 아닌 객체의 속성을 가져 오려고하면 -> get) 두 개의 쿼리를 먼저 컬렉션을 만들고, 병합 할 수 있습니다. – phobia82

+0

이 오류가 있습니다. 개체가 아닌 개체에 대한 merge() 멤버 함수 호출. 추가 할 때 -> get() 둘 다! – nabil