Laravel tips
3 tips · newest first
Short, practical Laravel tips: Eloquent performance, queues, testing helpers, and framework features most codebases underuse. Each tip is a copy-pasteable snippet with the why behind it.
Turn N+1 queries into errors in development with preventLazyLoading()
LARAVELCalled in a service provider, this throws a LazyLoadingViolationException whenever a relation is accessed without being…
eloquentperformance
@hnoozAuto-delete stale records with the Prunable trait instead of a custom command
LARAVELAdd Prunable and a prunable() query, and model:prune (scheduled) deletes matching rows in chunks — no bespoke cleanup c…
eloquentmaintenance
@hnoozUse whenLoaded() in API resources to avoid firing queries during serialization
LARAVELReferencing $this->posts directly in a resource lazy-loads the relation for every model when it wasn't eager-loaded — a…
eloquentapi-resources
@hnooz