Using Query Builder
$randomUser = DB::table('users') ->inRandomOrder() ->first();
Using Eloquent Model
$randomComment = Comment::query() ->inRandomOrder() ->first();
Both of the queries above will return a random record.
Learn how to get random record in Laravel, Make use of eloquent to get the random model and query builder to get random record
Created on Jul 06, 2021
•1396 views
$randomUser = DB::table('users') ->inRandomOrder() ->first();
$randomComment = Comment::query() ->inRandomOrder() ->first();
If you like our tutorial, do make sure to support us by being our Patreon or buy us some coffee ☕️
Collection of Tailwind CSS components for everyone to use. Browse all of the components that are right for your project.
View Components1 year ago
1 year ago
9 months ago
1 year ago
1 year ago
1 year ago