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
- The use statement with non-compound name 'Auth' has no effect
- How to create laravel project using composer
- How to insert dynamic values to additional column with pivot column in pivot table on multiple records
- Split an Eloquent Collection by half in Laravel
- Ignore Records where a field has NULL value in Laravel
- How to get query string value in laravel
- How to call Laravel route in jQuery
- Argument 1 passed to App\Http\Controllers\Auth\LoginController::authenticated() must be an instance of App\Http\Controllers\Auth\Request
- Print last executed query in laravel
- Laravel recursive function in controller
- How to run a specific seeder class in laravel
- How to delete record in Laravel with ajax
- Laravel 7 login error message not showing
- How to pass external link in laravel blade to anchor tag
- Laravel file size validation not working
- How to check duplicate entry in laravel
- How to pass query string with pagination in laravel
- How to get route name on visit URL in laravel
- Method Illuminate\Database\Eloquent\Collection::appends does not exist
- Insert current date time in a column using Laravel
- How to implement toggleLike() method in Overtrue\LaravelLike laravel package
- SQLSTATE[42S02]: Base table or view not found: 1146 Table 'laravel8.projects' doesn't exist
- How to pass data to route in laravel?
- Attempt to read property "avatar" on null in Laravel
- Drop foreign key column in Laravel using migration