Laravel: Get random model

A few ways of retrieving an random database record with Laravel Model.

--- views

Laravel get random model

Within Laravel there are many different ways to implement foreign keys. The way you choose to implement them doesn't matter, but it's important to be consistent.

So choose the one that works best for you and stick with it.

Code

class GenreBookFactory extends Factory { public function definition(): array { return [ "book_id" => Book::all()->random()->id ]; } }
php

However, there's a more effective approach! Import ComponentPropsWithoutRef and assign it to the element you want to set props for.

Code

class GenreBookFactory extends Factory { public function definition(): array { return [ "book_id" => Book::all()->random()->id ]; } }
php

This method enhances the development experience by letting TypeScript infer and enforce the correct props for the specified HTML element.