375

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?

Peter O.
  • 32,158
  • 14
  • 82
  • 96
DigitalWM
  • 4,406
  • 3
  • 18
  • 15

17 Answers17

863

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

D Malan
  • 10,272
  • 3
  • 25
  • 50
aebersold
  • 11,286
  • 2
  • 20
  • 29
  • 91
    Replace 'get' with 'first' if you want a single row. – Collin Price Feb 20 '14 at 19:11
  • 16
    for PostgreSQL use `'RANDOM()'` – dwenaus Nov 20 '14 at 16:01
  • 4
    Warning: on large datasets this is very slow, adding around 900 ms for me – S.. Jan 04 '15 at 22:34
  • 1
    This is a depends how the RDBMS implements RAND, on MySQL all rows are fetched and then one is picked. Which is pretty slow on big datasets. – aebersold Jan 05 '15 at 09:49
  • 3
    Can we paginate this? – Irfandi D. Vendy Sep 22 '15 at 10:03
  • 3
    You can, however the sorting will be random on every new page. Which makes no sense because it's essentially the same as you press F5. – aebersold Sep 22 '15 at 19:08
  • yeah , i used `User::orderBy(DB::raw('RAND()'))->get();` form my models, is the best for me – yussan Jun 03 '16 at 16:50
  • DB::raw('RAND()') slows down the query too much – CodeGuru Feb 23 '19 at 08:51
  • 1
    Caution, `User::all()->random();` will be slow on large datasets, prefer using `User::inRandomOrder()->get();`. As it will load ALL of your models in a php collection and then pick a random item. You may prefer a database approach without loading all your models. – Ifnot Jan 24 '20 at 12:36
  • DB::.... solution will return you "Class 'App\Http\Controllers\DB' not found" error so you will have to include DB as "use DB;" above your blablaController class code. Thanks a lot. – Kamlesh Apr 30 '21 at 05:01
  • Is there any way to randomised based on specific column. Using inRandomOrder() its randoming based on id. I need to randomised my data based on Zip Code. Is there any way to do that? – zia sultan Aug 23 '21 at 20:17
59

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.

Yevgeniy Afanasyev
  • 37,872
  • 26
  • 173
  • 191
Manish
  • 1,946
  • 2
  • 24
  • 36
41

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();
Gras Double
  • 15,901
  • 8
  • 56
  • 54
30

You can use:

ModelName::inRandomOrder()->first();
simhumileco
  • 31,877
  • 16
  • 137
  • 115
24

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();
Talles Airan
  • 341
  • 2
  • 3
19

In Laravel 4 and 5 the order_by is replaced by orderBy

So, it should be:

User::orderBy(DB::raw('RAND()'))->get();
Teodor Talov
  • 1,933
  • 2
  • 25
  • 39
11

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

Manuel Azar
  • 853
  • 2
  • 14
  • 24
9

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()'));
Bilal Gultekin
  • 4,831
  • 3
  • 22
  • 27
4

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();
simhumileco
  • 31,877
  • 16
  • 137
  • 115
hosein azimi
  • 321
  • 3
  • 5
4

Use Laravel function

ModelName::inRandomOrder()->first();
Eric Aya
  • 69,473
  • 35
  • 181
  • 253
Kamlesh Paul
  • 11,778
  • 2
  • 20
  • 33
3

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

simhumileco
  • 31,877
  • 16
  • 137
  • 115
ctf0
  • 6,991
  • 5
  • 37
  • 46
3

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.

Brad Ahrens
  • 4,864
  • 5
  • 36
  • 47
2

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();
Neto Braghetto
  • 1,371
  • 1
  • 15
  • 30
2

I prefer to specify first or fail:

$collection = YourModelName::inRandomOrder()
  ->firstOrFail();
giovannipds
  • 2,860
  • 2
  • 31
  • 39
2

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.

hackernewbie
  • 1,606
  • 20
  • 13
1

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(); 
Doc
  • 177
  • 1
  • 3
  • 14
  • The problem with this is that if there are multiple rows with ids greater than `$count` only the first of these would ever be retrieved, and so it would also be more likely to be retrieved than any other row. – kemika Sep 05 '17 at 23:27
0

Try this code! It Works:

  User::orderBy(DB::raw('RAND()'))->get();
  • 1
    Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Oct 24 '21 at 07:35