Stop stuffing regex into strings. Build reusable, testable validation rules with invokable Rule objects and closures.
Tips
Calling ->get() on a giant table loads every row into memory at once. Laravel gives you chunk, chunkById, cursor, and lazy to process big result sets safely.
Calling env() in a controller works in development and returns null in production. Here is why config caching breaks it and the one rule that avoids the trap.
Wrapping related writes in a transaction is easy to get subtly wrong. Laravel's DB::transaction() closure handles commit, rollback, and deadlock retries for you.
Laravel's Stringable class turns nested PHP string functions into a readable left-to-right chain. Here are the methods worth committing to memory.
Stop wrapping query builder calls in if statements. The when() and unless() methods let you apply constraints conditionally while keeping the chain fluent.
Eloquent will happily hide an N+1 query until it hits production. Strict model mode turns those silent mistakes into loud exceptions during development.
A handful of Artisan optimization commands shave real milliseconds off every request in production. Here is what each one does and when to run it.
Factory states, relationships, and sequences let you express exactly the test data a scenario needs in a single readable line.
Named rate limiters and the RateLimiter facade let you throttle routes and any arbitrary action with per-user, per-plan, or dynamic limits.
Two small Laravel features, the tap() helper and higher-order collection messages, quietly remove a surprising amount of boilerplate from everyday code.
Cast Eloquent attributes to backed PHP enums to replace magic strings with real types, and give your enums behavior with helper methods.