Maximum execution time exceeded in Laravel: chunk, queue or optimise
The request genuinely ran too long. Raising the limit is the worst of the three fixes; here is how to decide between optimising, chunking and queueing.
A 30-second timeout is PHP saying the request exceeded max_execution_time. You can raise it, but a web request that takes 30 seconds is a design problem, and the fix usually belongs in a job.
Find what is actually slow
Enable the query log or a profiler and look for the loop. In our experience the culprits are an import that saves row by row, an API call inside a foreach, or an unindexed query returning a huge table. Fix that first — the timeout is a symptom.
Move long work into a queued job
Sending 500 emails, generating PDFs or syncing an external API does not belong in the request cycle. Dispatch a job, return a 202, and let the worker take as long as it needs. This also makes retries and progress tracking possible.
Chunk large datasets
When the work must stay in a command, use chunkById(500) rather than get(). Memory stays flat and each batch is a separate unit you can log, resume or retry. Add --timeout to long Artisan commands so the CLI limit does not surprise you.
If you must raise the limit
Set it per script with set_time_limit(120) or in the PHP-FPM pool, never globally for the whole site. A higher limit only buys time; it does not make the request faster, and it holds a PHP worker that other requests need.
Frequently asked questions
Why does it happen only for one user?
That user probably triggers a report or export that scans a large table. Check their account for an unusually large dataset before changing global settings.
Does increasing max_execution_time fix timeouts in Nginx?
No. Nginx has its own fastcgi_read_timeout, and the load balancer in front of it has another. All three must agree, which is another reason to move the work to a queue.
Can a slow query alone cause this?
Yes, if it scans millions of rows without an index. Run EXPLAIN on the query and add the composite index the WHERE and ORDER BY clauses actually need.
More on this topic: Laravel articles.
Admin
Related articles
Upgrading Laravel 10 to 12: a realistic one-week checklist
Two major versions at once is mostly mechanical: dependency updates, the new bootstrap file, and a test suite that tells you what...
Livewire or Inertia with Vue/React: which is cheaper to build and keep?
Livewire keeps one language and one deploy; Inertia buys a real component ecosystem at the cost of a second build pipeline. The ch...