Trial expirations, subscription renewals, and rate limit windows all depend on "now." Here is how to control it in tests instead of fighting it.
Tips
Looping over a collection to count or sum related rows is a classic N+1. Eloquent has a single-query answer for every common case.
Stop repeating the same ->where() clause on every query. Global scopes bake query rules directly into the model.
When a dataset is too large to hold in memory as a Collection, LazyCollection processes it one item at a time using generators.
Stop stuffing regex into strings. Build reusable, testable validation rules with invokable Rule objects and closures.
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.