laravel
✓Verified·Scanned 2/18/2026
Avoid common Laravel mistakes — N+1 queries, mass assignment, cache gotchas, and queue serialization traps.
from clawhub.ai·vbbbb2d1·2.9 KB·0 installs
Scanned from 1.0.0 at bbbb2d1 · Transparency log ↗
$ vett add clawhub.ai/ivangdavila/laravel
Eloquent N+1
- Accessing relationship in loop without eager load —
User::with('posts')->get()notUser::all()then->posts - Nested relationships need dot notation —
with('posts.comments')for eager loading withCount('posts')for counting without loading — addsposts_countattributepreventLazyLoading()in AppServiceProvider — crashes on N+1 in dev, catches bugs early
Mass Assignment
$fillablewhitelist OR$guardedblacklist — not both$guarded = []allows all fields — dangerous, prefer explicit$fillablecreate()andupdate()respect mass assignment —$model->field = xbypasses it- Request validated data is not auto-safe — still filtered by fillable/guarded
Cache Pitfalls
config:cachebakes .env values — env() only works in config files after cachingroute:cacherequires all routes to be controller-based — no closuresphp artisan optimizecombines config, route, view cache — run after deploy- Local changes not reflecting —
php artisan cache:clear && config:clear && route:clear
Queue Jobs
- Job class properties serialized — models serialize as ID, re-fetched on process
- Closure can't be queued — must be invocable class
- Failed jobs go to
failed_jobstable — check there for errors $tries,$timeout,$backoffas job properties — or in config- Connection vs queue: connection is driver, queue is named channel on that driver
Middleware
- Order matters — earlier middleware wraps later
$middlewareglobal on every request —$middlewareGroupsfor web/api- Terminate middleware runs after response sent — for logging, cleanup
- Route middleware can have parameters —
role:adminpasses 'admin' to middleware
Database
migrate:freshdrops ALL tables —migrate:refreshrolls back then migratesDB::transaction()auto-rolls back on exception — but not on manualexitor timeout- Soft deletes excluded by default —
withTrashed()to include firstOrCreatevsfirstOrNew— first persists, second doesn't
Testing
RefreshDatabaseis faster thanDatabaseMigrations— uses transactions- Factories:
create()persists,make()doesn't — use make for unit tests $this->withoutExceptionHandling()shows actual errors — helpful for debugging- Queue fake:
Queue::assertPushed()— check job was queued without running it
Common Mistakes
find()returns null,findOrFail()throws 404 — use OrFail to avoid null checksenv()in cached config returns null — only use env() inside config files- Validation
requireddoesn't mean non-empty — userequired|filledfor strings - Route model binding uses
idby default —getRouteKeyName()to change