samedi 16 décembre 2017

Laravel ORDER BY rand FROM last [..] records

I have this code, yes, it show random, but how to make random from LAST 30 records from table, and if the table will dont have enough records dont be error?

    $galleries = DB::table('galleries')->select( DB::raw('galleries.*') )
        ->where( 'domain_id', $domain_id )
        ->where( 'is_deleted', 0 )
        ->whereNotIn('id', array_keys( Session::get("export_selected"  ) ) )
        ->whereNotIn('id',             Session::get("export_skip"      )   )
        ->whereNotIn('id', array_keys( Session::get("export_delete"    ) ) )
        ->whereNotIn('id', array_keys( Session::get("export_delete_all") ) )
        ->leftJoin('history', function($join) use ( $site_id ) {
            $join->where('history.site_id', '=', $site_id )
                 ->on('history.gallery_id', '=', 'galleries.id');
        })
        ->whereNull('history.gallery_id')
        ->orderByRaw('RAND()')
        ->take(10)->get();




Aucun commentaire:

Enregistrer un commentaire