Randomly display data from a database

3

I would like to stop displaying by date and descending and make it display randomly or as I know it - random.

Below is the excerpt that I have to modify from the php file:

public function getNewReleases() {
        return Album::with('artist', 'tracks')
            ->join('artists', 'artists.id', '=', 'albums.artist_id')
            ->orderBy('release_date', 'desc')
            ->limit(40)
            ->select('albums.*')
            ->get();
    }
}
    
asked by anonymous 14.09.2017 / 16:30

1 answer

5

You can use ORDER BY RAND() depending on your version:

Laravel >= 5.2:

User::inRandomOrder()->get();
Laravel 4.2.7 - 5.1:

User::orderByRaw("RAND()")->get();
Laravel 4.0 - 4.2.6:

User::orderBy(DB::raw('RAND()'))->get();
Laravel 3:

User::order_by(DB::raw('RAND()'))->get();
    
14.09.2017 / 16:38