nabil nabil - 1 year ago 85
PHP Question

laravel result of 2 objects in one array

I have 2 objects, the first retrieves the comments of the object work and the second retrieves the comments of the object achievement. I want to display the results in a single table containing the list of comments.

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

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

in view :

@foreach($posts as $post)

Answer Source

If you are not using Laravel 5.3, using get will return arrays of results as mentioned by @iCode, then you can use the native array_merge.

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

or add the get() method to your queries and just do

$posts = array_merge($posts1,$posts2);
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download