Laravel - Eloquent or Fluent random row

PhpLaravelRandomEloquentFluent

Php Problem Overview


How can I select a random row using Eloquent or Fluent in Laravel framework?

I know that by using SQL, you can do order by RAND(). However, I would like to get the random row without doing a count on the number of records prior to the initial query.

Any ideas?

Php Solutions


Solution 1 - Php

Laravel >= 5.2:

User::inRandomOrder()->get();

or to get the specific number of records

// 5 indicates the number of records
User::inRandomOrder()->limit(5)->get();
// get one random record
User::inRandomOrder()->first();

or using the random method for collections:

User::all()->random();
User::all()->random(10); // The amount of items you wish to receive

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();

Check this article on MySQL random rows. Laravel 5.2 supports this, for older version, there is no better solution then using RAW Queries.

edit 1: As mentioned by Double Gras, orderBy() doesn't allow anything else then ASC or DESC since this change. I updated my answer accordingly.

edit 2: Laravel 5.2 finally implements a wrapper function for this. It's called inRandomOrder().

Solution 2 - Php

This works just fine,

$model=Model::all()->random(1)->first();

you can also change argument in random function to get more than one record.

Note: not recommended if you have huge data as this will fetch all rows first and then returns random value.

Solution 3 - Php

tl;dr: It's nowadays implemented into Laravel, see "edit 3" below.


Sadly, as of today there are some caveats with the ->orderBy(DB::raw('RAND()')) proposed solution:

  • It isn't DB-agnostic. e.g. SQLite and PostgreSQL use RANDOM()

  • Even worse, this solution isn't applicable anymore since this change:

    $direction = strtolower($direction) == 'asc' ? 'asc' : 'desc';


edit: Now you can use the orderByRaw() method: ->orderByRaw('RAND()'). However this is still not DB-agnostic.

FWIW, CodeIgniter implements a special RANDOM sorting direction, which is replaced with the correct grammar when building query. Also it seems to be fairly easy to implement. Looks like we have a candidate for improving Laravel :)

update: here is the issue about this on GitHub, and my pending pull request.


edit 2: Let's cut the chase. Since Laravel 5.1.18 you can add macros to the query builder:

use Illuminate\Database\Query\Builder;

Builder::macro('orderByRandom', function () {

    $randomFunctions = [
        'mysql'  => 'RAND()',
        'pgsql'  => 'RANDOM()',
        'sqlite' => 'RANDOM()',
        'sqlsrv' => 'NEWID()',
    ];

    $driver = $this->getConnection()->getDriverName();

    return $this->orderByRaw($randomFunctions[$driver]);
});

Usage:

User::where('active', 1)->orderByRandom()->limit(10)->get();

DB::table('users')->where('active', 1)->orderByRandom()->limit(10)->get();


edit 3: Finally! Since Laravel 5.2.33 (changelog, PR #13642) you can use the native method inRandomOrder():

User::where('active', 1)->inRandomOrder()->limit(10)->get();

DB::table('users')->where('active', 1)->inRandomOrder()->limit(10)->get();

Solution 4 - Php

You can use:

ModelName::inRandomOrder()->first();

Solution 5 - Php

In Laravel 4 and 5 the order_by is replaced by orderBy

So, it should be:

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

Solution 6 - Php

it's very simple just check your laravel version

Laravel >= 5.2:

User::inRandomOrder()->get();
//or to get the specific number of records
// 5 indicates the number of records
User::inRandomOrder()->limit(5)->get();
// get one random record
User::inRandomOrder()->first();

or using the random method for collections:

User::all()->random();
User::all()->random(10); // The amount of items you wish to receive

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();

Solution 7 - Php

You can also use order_by method with fluent and eloquent like as:

Posts::where_status(1)->order_by(DB::raw(''),DB::raw('RAND()')); 

This is a little bit weird usage, but works.

Edit: As @Alex said, this usage is cleaner and also works:

Posts::where_status(1)->order_by(DB::raw('RAND()'));

Solution 8 - Php

For Laravel 5.2 >=

use the Eloquent method:

inRandomOrder()

The inRandomOrder method may be used to sort the query results randomly. For example, you may use this method to fetch a random user:

$randomUser = DB::table('users')
            ->inRandomOrder()
            ->first();

from docs: https://laravel.com/docs/5.2/queries#ordering-grouping-limit-and-offset

Solution 9 - Php

You can easily Use this command:

// Question : name of Model
// take 10 rows from DB In shuffle records...

$questions = Question::orderByRaw('RAND()')->take(10)->get();

Solution 10 - Php

Use Laravel function

ModelName::inRandomOrder()->first();

Solution 11 - Php

I prefer to specify first or fail:

$collection = YourModelName::inRandomOrder()
  ->firstOrFail();

Solution 12 - Php

Laravel has a built-in method to shuffle the order of the results.

Here is a quote from the documentation:

shuffle()

The shuffle method randomly shuffles the items in the collection:

$collection = collect([1, 2, 3, 4, 5]);

$shuffled = $collection->shuffle();

$shuffled->all();

// [3, 2, 5, 1, 4] - (generated randomly)

You can see the documentation here.

Solution 13 - Php

There is also whereRaw('RAND()') which does the same, you can then chain ->get() or ->first() or even go crazy and add ->paginate(int).

Solution 14 - Php

At your model add this:

public function scopeRandomize($query, $limit = 3, $exclude = [])
{
    $query = $query->whereRaw('RAND()<(SELECT ((?/COUNT(*))*10) FROM `products`)', [$limit])->orderByRaw('RAND()')->limit($limit);
    if (!empty($exclude)) {
        $query = $query->whereNotIn('id', $exclude);
    }
    return $query;
}

then at route/controller

$data = YourModel::randomize(8)->get();

Solution 15 - Php

I have table with thousands of records, so I need something fast. This is my code for pseudo random row:

// count all rows with flag active = 1
$count = MyModel::where('active', '=', '1')->count(); 

// get random id
$random_id = rand(1, $count - 1);  

// get first record after random id
$data = MyModel::where('active', '=', '1')->where('id', '>', $random_id)->take(1)->first(); 

Solution 16 - Php

Try this code! It Works:

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

Solution 17 - Php

Here's how I get random results in eloquent in one of my projects:

$products           =  Product::inRandomOrder()->limit(10);

10 - The number of random records to pull.

Solution 18 - Php

In Laravel 7.x and above, you can just do:

$data = Images::all()->random(4);

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionDigitalWMView Question on Stackoverflow
Solution 1 - PhpaebersoldView Answer on Stackoverflow
Solution 2 - PhpManishView Answer on Stackoverflow
Solution 3 - PhpGras DoubleView Answer on Stackoverflow
Solution 4 - PhpsimhumilecoView Answer on Stackoverflow
Solution 5 - PhpTeodor TalovView Answer on Stackoverflow
Solution 6 - PhpTalles AiranView Answer on Stackoverflow
Solution 7 - PhpBilal GultekinView Answer on Stackoverflow
Solution 8 - PhpManuel AzarView Answer on Stackoverflow
Solution 9 - Phphosein azimiView Answer on Stackoverflow
Solution 10 - PhpKamlesh PaulView Answer on Stackoverflow
Solution 11 - PhpgiovannipdsView Answer on Stackoverflow
Solution 12 - PhpBrad AhrensView Answer on Stackoverflow
Solution 13 - Phpctf0View Answer on Stackoverflow
Solution 14 - PhpNeto BraghettoView Answer on Stackoverflow
Solution 15 - PhpKrzysztof ChełchowskiView Answer on Stackoverflow
Solution 16 - Phpuser17207600View Answer on Stackoverflow
Solution 17 - PhphackernewbieView Answer on Stackoverflow
Solution 18 - PhpJustice Selorm BruceView Answer on Stackoverflow