2016-11-26 38 views
1

我有2个对象,第一个检索对象工作的注释,第二个检索对象成果的注释。我想在包含评论列表的单个表格中显示结果。laravel 2个对象在一个数组中的结果

$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

相关问题