What is the HasUuids trait in Laravel?
Why Interviewers Ask This
This is a differentiating question used for senior and lead roles. Interviewers want to see if you can explain not just what happens, but why — and what the trade-offs are in different approaches.
Answer
The HasUuids trait (Laravel 9+) automatically generates UUID (Universally Unique Identifier) primary keys for Eloquent models instead of auto-incrementing integers. Add the trait to your model: use HasUuids;. Create the column as uuid type in migration: $table->uuid("id")->primary(). When you create a model, Eloquent automatically generates and sets a UUID: User::create(["name" => "Alice"]) gets a UUID like 550e8400-e29b-41d4-a716-446655440000. Generate UUID for specific columns: public function uniqueIds(): array { return ["id", "invitation_token"]; }. Benefits of UUIDs: no sequential IDs leaking business information, safe to generate on the client side, merge-friendly for distributed systems. Downside: slightly larger storage and slower indexed lookups than integers. Use HasUlids for ULID support (sortable, URL-safe unique IDs).
Pro Tip
Demonstrate both theoretical understanding and practical experience. Say what it is, then give an example of how you actually used it in a Laravel codebase.