Currently, in my ModelFactory.php, I have:
$factory->define(App\\Reply::class, function (Faker\\Generator $faker) {
return [
\'thread_id\' => 1,
Any class that extends Illuminate\Database\Eloquent\Model
will be able to do this:
User::inRandomOrder()->first()
Or to get a Collection of 3 items:
User::inRandomOrder()->limit(3)->get()
This might be more efficient than using User::all()->first()
, which ought to first query all Users.
Your IDE (like PhpStorm) will probably be wildly confused that this is an option though.
Also see: Laravel - Eloquent or Fluent random row