Currently Available: Need a skilled Software Developer for your next project?
Categories
Laravel

What the Laravel Docs Don’t Tell You

Laravel's documentation explains how its APIs work, yet your production app also depends on how Laravel interacts with your deployment steps, database, storage provider, and queue workers. For example, when you run php artisan route:cache, Laravel saves a compiled list of routes and loads it for incoming requests. If you later edit your route files, Laravel does not refresh that cache on its own.

To avoid surprises in production, you need to pair each API with the operational choices around it. You decide which validated input your code saves, how your app handles a failed file write, when a worker can pick up a queued email, and whether an HTTP failure gives you a response object or throws an exception. This article covers Laravel 13 and highlights version-specific changes that can catch you during an upgrade.

Routing depends on parameter order and deployment state

Laravel passes route parameters to a callback or controller action in the order they appear in the URL. If Laravel also injects a dependency from its service container, you must place that dependency before any route parameters in the method signature. Changing the argument names does not change that order. By default, a route parameter matches only a single segment of the URL path, so you need to configure the route explicitly if you want a parameter to accept a slash.

With implicit model binding, Laravel fetches an Eloquent model for a route parameter whenever your action type-hints a model variable with the same name as that parameter. If Laravel cannot find a matching record, it returns a 404 response. You can also tell a route to look up a record by another column, such as a slug, instead of the default ID. Model binding only locates the record; it does not check whether the current user is allowed to see or change it, so you still need to run your own authorization checks. The routing documentation covers these rules in detail.

During deployment, php artisan route:cache writes the compiled route table that Laravel loads to handle requests. Laravel recommends building this cache as part of your deployment step. Whenever you edit route definitions, you have to rebuild the cache, and you should test the cached routes during your release checks before traffic hits them.

If you are upgrading to Laravel 13, check any routes that specify a domain. Laravel 13 matches domain-specific routes before routes without a domain, which can change which route handles a request in apps that relied on registration order. The Laravel 13 upgrade guide calls out this change.

Validation controls which input reaches your code

An incoming request carries every field the client sent, whereas validated data contains only the fields that passed your rules. Instead of passing raw request data to code that writes to the database, pass validated() or a narrowed result from safe(). safe() returns a ValidatedInput object with methods like only, except, and all. If you use conditional exclude rules, Laravel strips matching fields from the validated output. You can read the full details in Laravel's validation documentation.

Test optional fields directly. By default, Laravel skips ordinary validation rules when a field is missing or contains an empty string. If you want a custom rule to run on a missing or empty value, make it an implicit rule. An implicit rule treats the field as required and lets your rule's own logic decide whether validation fails. Decide how each field should behave when it is missing, empty, or null, and write a test for each case.

Rules that query the database have clear limits. exists checks whether the submitted values appear in a given table and column. unique checks a column and includes soft-deleted records unless you chain withoutTrashed(), and you can add extra conditions like an account ID with where(). Because these rules only check the database at the moment the request runs, they cannot stop two conflicting writes that arrive at the same time. You still need database constraints to protect your data.

When you handle file uploads, do not rely on the filename extension alone to verify a file's type. Laravel recommends pairing the extensions rule with mimes or mimetypes. Also keep in mind that the image rule rejects SVG files by default because SVGs can carry cross-site scripting risks. Laravel 12 documented this same default, so check the Laravel 12 upgrade guide before assuming an older app still accepts SVG uploads.

Storage writes can fail silently without a clear policy

When a Laravel storage write fails, it can return false without throwing an exception or writing anything to your logs. Your filesystem configuration controls what Laravel does next. If you set throw to true, Laravel throws League\Flysystem\UnableToWriteFile on a failed write. If you configure report, Laravel logs the underlying exception but still returns a value instead of throwing. Check the return value in your code, or configure and test the failure behavior for each disk you use. Laravel documents these options in its filesystem reference.

Laravel resolves every storage path relative to the root directory configured for that disk. For the local disk, Laravel documents storage/app/private as the default root, so it resolves local paths from there. Where you store a file and who can access it are two separate concerns. Laravel offers public and private visibility as cross-platform labels, yet different storage providers enforce permissions in their own way.

Temporary URLs also depend on the disk you choose. Laravel documents temporary URL support for local and S3 disks. If you are working on an older app, you may need to set serve => true in your local disk configuration before Laravel can generate local temporary URLs. Verify how your specific provider and environment behave, since URL generation and visibility rules vary across setups.

Lazy collections do not undo eager memory use

A standard Collection already holds all of its items in memory. When you call collect($items)->lazy(), Laravel returns a LazyCollection that can keep memory lower during later transformations, but the original collection still holds its values. If you want to stream data from the start, write a generator or fetch database records in batches. Laravel explains the difference in its collection documentation.

Eloquent's cursor() returns a lazy collection and hydrates one model at a time, though it cannot eager-load relationships. Under the hood, your database driver's PDO layer still buffers the raw query results in memory. On very large result sets, those buffered rows can still exhaust memory, which is why Laravel recommends lazy() as an alternative. If a job updates the same column it uses to filter records, use lazyById() so Laravel fetches each chunk using the ID of the last model and avoids processing the same rows twice. Pick your method based on whether you need eager-loaded relationships, how many records you expect, and whether the job modifies its filter column. Eloquent's iteration guidance covers these methods.

Queued mail can run before a database transaction commits

When you call queue(), Laravel pushes a mailable onto a queue for background processing. Laravel also queues any mailable that implements ShouldQueue even when your code calls send(). Because queuing a message does not deliver it right away, you need a running queue worker to process the job.

Database transactions can introduce a race condition here. If your code queues a mailable inside a transaction before it commits, a worker can pick up the job before the new or updated records exist in the database. When your queue connection's after_commit setting is false, call afterCommit() when sending the message or set it in the mailable's constructor. Laravel will then hold the job until the database transaction commits. The mail documentation covers queueing and transaction timing.

HTTP status errors and connection failures behave differently

Laravel's HTTP client does not throw an exception when a server returns a 4xx or 5xx status code. You have to check the response yourself with methods like failed(), clientError(), or serverError(). A timeout or other network failure works differently: the client can throw Illuminate\Http\Client\ConnectionException instead. The HTTP client documentation explains how these two paths differ.

By default, the HTTP client waits up to 30 seconds for a response and up to 10 seconds to establish a connection. You can adjust the response wait with timeout() and the connection wait with connectTimeout(). Set both values to match the external service you are calling and the kind of request you are making.

Define a clear retry policy before you rely on automatic retries. retry() takes a maximum number of attempts and a delay, and you can also pass a callback to decide whether Laravel should retry a given failure. By default, Laravel throws a request exception if every attempt fails. Passing throw: false makes the client return the last HTTP response instead, though Laravel will still throw a ConnectionException if every attempt fails before receiving any response. When you send pooled requests, check the type of each item in the response array before calling response methods on it, because a failed connection shows up as a ConnectionException in that array.

Check your Laravel version before relying on defaults

Laravel 13 requires PHP 8.3 or later. Under Laravel's support policy, each major version receives bug fixes for 18 months and security fixes for two years. According to the release notes, Laravel 12 came out on February 24, 2025, with bug fixes through August 13, 2026 and security fixes through February 24, 2027. Laravel 13 came out on March 17, 2026, with bug fixes through Q3 2027 and security fixes through March 17, 2028. Those dates only cover Laravel itself; they do not cover your app's other dependencies or hosting environment.

Laravel 13's new application skeleton defaults session serialization to JSON. If you switch an existing app from PHP serialization to JSON, Laravel invalidates all active sessions. Stay on PHP serialization if you need to keep current users signed in. Laravel recommends switching to JSON when your app does not store PHP objects in sessions and users can sign in again. The upgrade guide lays out this tradeoff.

What I'm building

Delegate tasks. Get software.

Give Vroni a GitHub issue, bug report, spec, or rough idea. It reads the repo, plans the change, writes code, runs checks, and works toward a review-ready pull request.

Take a look at vroni.com

Email updates

Usually a new article and a few links I found interesting.

No spam. Unsubscribe with one click.

Leave a Reply

Your email address will not be published. Required fields are marked *