vendredi 30 décembre 2016

laravel how sort merge collections

I have 3 query, my problem is I want the merged collection to be sorted by created_at.

How to merge and sort three qurey-builder Collections?

        $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');



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire