Search
 
SCRIPT & CODE EXAMPLE
 

PHP

eloquent get random

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

PREVIOUS NEXT
Code Example
Php :: get full url php 
Php :: how to get page name in php 
Php :: how to get file extension in laravel 
Php :: how to migrate just one table in laravel 
Php :: php remove enter from string 
Php :: wordpress get theme uri 
Php :: laravel password confirmation 
Php :: wordpress debug mode enabled 
Php :: wordpress check user against user roles 
Php :: rout debug symfony command 
Php :: write to file php 
Php :: store image in public folder laravel 8 
Php :: php get next saturday 
Php :: laravel random query 
Php :: wordpres get_posttype 
Php :: grep only in php files 
Php :: create laravel project with composer 
Php :: phpexcel set font color 
Php :: how to remove text tab in wordpress editor 
Php :: php array remove empty values 
Php :: php file_get_contents disable ssl check 
Php :: close mysql connection in php 
Php :: function exists php 
Php :: get first key of array php 
Php :: add 7 days to date php 
Php :: laravel publish email template 
Php :: laravel old request htmlselect 
Php :: php mysql count rows 
Php :: php mkdir if not exists 
Php :: php get current year 
ADD CONTENT
Topic
Content
Source link
Name
5+1 =