Interview frequency: …
Short answer
They add a deleted_at column and automatically exclude deleted records with a Global Scope; include deleted_at in composite indexes to keep queries fast.
Explanation
With SoftDeletes, the row is not removed from disk; its deletion time is stored in deleted_at. Eloquent automatically adds WHERE deleted_at IS NULL to each query. In tables with millions of rows, indexes that omit deleted_at may become less efficient and force the engine to scan the entire table (Full Table Scan).
Code example
// Use soft deletes
use SoftDeletes;
// Restore deleted records
User::withTrashed()->where('id', 1)->restore();Common mistakes
- Ignoring the Unique Constraint with Soft Deletes: a new user cannot reuse an email address that belongs to a soft-deleted user unless deleted_at is included in the constraint.
Follow-up questions
- How do Local Scopes and Global Scopes differ in Eloquent models, and how do you implement them?
- What are Eloquent model events, and how do Observers organize business logic?