Laravel - Eloquent or Fluent random row
In Laravel's Eloquent ORM, you can use the inRandomOrder
method to retrieve a random row from the database. Here's an example:
$randomRow = Model::inRandomOrder()->first();
This will retrieve a random row from the Model
table and assign it to the $randomRow
variable.
Alternatively, you can use the DB::raw
method to use a raw expression to select a random row:
$randomRow = Model::orderBy(DB::raw('RAND()'))->first();
This will also retrieve a random row from the Model
table.
Please let us know if you have any other questions!