laravel结果是一个数组中的2个对象

时间:2016-11-26 14:24:02

标签: php sql laravel eloquent query-builder

我有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

2 个答案:

答案 0 :(得分:2)

这可能有很多不足之处,但最简单的就是merge对象为:

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

<强>更新

尝试合并为:

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

答案 1 :(得分:0)

如果您没有使用Laravel 5.3,使用get将返回@iCode提到的结果数组,那么您可以使用本机array_merge。

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

或将get()方法添加到您的查询中,然后执行

$posts = array_merge($posts1,$posts2);
相关问题