How to get records in random order in laravel
How to get records in random order in laravel
In this session, we will try our hand at solving the "How to get records in random order in laravel".
You can get the records in random order from a table in laravel. Laravel uses the inRandomOrder() method on eloquent collection to get the records in random order.-
Get records from user table in random order using inRandomOrder() in laravel
$users = User::inRandomOrder()->get();
Step by step code implementation
routes\web.php
use App\Http\Controllers\HomeController; Route::get('/get-data-in-random-order', [HomeController::class, 'getDataInRandomOrder']);
//Create HomeController using artisan command
php artisan make:controller HomeController
<?php namespace App\Http\Controllers; use Illuminate\Http\Request; use App\Models\User; class HomeController extends Controller { public function getDataInRandomOrder(){ $users = User::select('id', 'name')->inRandomOrder()->get(); echo "<pre>"; print_r($users); } }
You can get all the records from the user table in Laravel in random order. This code snippet will return all the records in a different order on each reload of the page.
-
Raw expression to get random records in laravel
$users = User::orderBy(DB::raw('RAND()'))->take(10)->get(); //Or $users = User::orderByRaw('RAND()')->take(10)->get();
You can get the records from users table in random order by using raw expressions.
If you like what you are reading, please consider buying us a coffee ( or 2 ) as a token of appreciation.
Don't forget to share this article! Help us spread the word by clicking the share button below.
We appreciate your support and are committed to providing you valuable and informative content.
We are thankful for your never ending support.
Random Code Snippet Queries: Laravel
- Touch parent updated_at in Laravel
- How to fetch single row data from database in laravel
- Laravel specific table Migration
- Split an Eloquent Collection by half in Laravel
- Credit card validation in laravel
- Create user in Laravel using tinker
- SQLSTATE[42000]: Syntax error or access violation: 1055
- Cannot end a section without first starting one
- Laravel save object to database
- Session Doesn't Work on Redirect
- How to get last record from object collection in laravel
- Generate random string lowercase in Laravel
- How to display HTML tags In Laravel blade
- How to pass data to partial view file in laravel
- Drop foreign key column in Laravel using migration
- Declaration of App\Models\Post::sluggable() must be compatible with Cviebrock\EloquentSluggable\Sluggable
- Argument 1 passed to App\Http\Controllers\Auth\LoginController::authenticated() must be an instance of App\Http\Controllers\Auth\Request
- Print query in laravel
- Comment .env file in laravel
- Retain selected value of select box in Laravel
- How to call model in blade laravel
- Print last executed query in laravel
- External link not working in laravel blade
- How to create projects method with belongstomany relationship in user model
- How to create new user without form submission in laravel