Display data randomly from a database

Asked

Viewed 89 times

3

I’d like to stop displaying by date and decreasing and make it display randomly or as I know it - randomly.

below the excerpt 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();
    }
}
  • There is no missing tag ?

  • i just need to change that stretch - ->orderby('release_date', 'desc') already put it growing and it worked, but wanted it random this information =)

  • My answer helped you?

  • @durtto didn’t work no

1 answer

5

You can use ORDER BY RAND() depends 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();
  • Nothing worked using what you suggested.

  • Then demonstrate what you tried, and put the result.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.