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
•1891 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 Components2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
1 year ago