Best Practices for Documenting Laravel Code
Laravel relies on PHP features and framework conventions, so a method’s signature (its name, parameters, and return type) does not always show…
Laravel is a PHP framework for building web services and websites. You can use its built-in conventions to handle common tasks like receiving HTTP requests, querying databases, and sending responses. Eloquent, Laravel’s object-relational mapper (ORM), lets your PHP code work with database records through model classes.
If you are building with Laravel in 2025, start with the tools the framework already includes for request validation, authorization, database access, queues, and testing. Keep each part of your application focused on a clear job, and wait to add extra architectural layers or performance tuning until your code gives you a concrete reason. Laravel 12 arrived on February 24, 2025, so the version advice below uses that release as its reference point.
Check Laravel’s support policy and PHP compatibility before you start a project or plan an upgrade. Laravel releases major versions generally once a year, and the maintainers provide 18 months of bug fixes and two years of security fixes for each one. When Laravel 12 launched in 2025, it supported multiple PHP versions. Confirm the exact compatibility for the versions you plan to deploy in the Laravel release notes and the PHP supported versions list.
A new major version can introduce breaking changes, so schedule the upgrade as dedicated maintenance instead of treating it like a drop-in replacement. Run your test suite against the new version and check your third-party dependencies for compatibility before you deploy. If you want a refresher on Laravel’s core conventions, read getting started with routing and Eloquent.
A controller is easiest to follow when it sticks to the HTTP boundary. It receives a request, passes trusted input to the code that does the work, and returns a response. Laravel Form Requests move validation rules and authorization checks into a dedicated request class. Validation checks whether the submitted data follows your rules, while authorization checks whether the current user is allowed to perform the action.
Keep those two checks separate in your head even when one Form Request holds both. Your application still needs to reject a request with valid fields if the user lacks permission to change the target record. Laravel’s validation documentation shows how Form Requests define both rules and authorization.
Once Laravel validates the request, pass only the validated data to the code that creates or updates a model:
$post = Post::create($request->validated());
Eloquent’s mass-assignment controls decide which model attributes Laravel accepts through methods like create and update. List the allowed attributes in $fillable or protect them with $guarded, and avoid passing the full request payload straight to a model. A field that passes validation is not automatically a field the user should be allowed to set. You can review these controls in the Eloquent mass-assignment API.
Keep straightforward behavior in the controller or in a small, focused class. Introduce a service or action class when substantial business logic needs a clear home or when multiple parts of the application share it. Laravel’s conventions give you plenty of structure on their own, so you do not need a repository or service layer for every model and controller.
Watch out for N+1 queries. This happens when your code loads a collection of records and then runs another database query for a relationship on every item in that collection. As the collection grows, the number of queries grows right along with it.
If a page displays each post’s author, load the authors alongside the posts instead of fetching each author lazily inside a loop:
$posts = Post::with('author')->latest()->paginate();
Eloquent eager loading fetches the requested relationships together with the main records. Laravel’s relationship documentation shows how eager loading can shrink a books-and-authors example down to two queries. Load only the relationships that the page or job actually uses, and inspect your real database queries before you optimize further. The Eloquent relationship documentation explains eager loading and the N+1 problem in detail.
A queue lets a web request hand slow work to a separate worker process so the user does not have to wait for a response. File processing is a common example. Laravel supports several queue backends, including database, Redis, and Amazon SQS, so pick the one that fits your infrastructure and operational needs.
Adding a queue also brings new failure modes and deployment steps. A worker retries jobs according to its configuration, so write jobs that can run more than once without corrupting data whenever you can. If a job depends on records created inside a database transaction, dispatch it after the transaction commits. Otherwise, a worker can pick up the job before the database has saved those records. Laravel documents the after_commit option and worker behavior in its queue documentation.
Queue workers are long-running processes. Because they do not automatically load newly deployed code, you need to restart them during deployment and use a process manager to bring them back up. Set the worker timeout several seconds shorter than the queue connection’s retry_after value. If you do not, the queue can release a job back to another worker while the first worker is still running it, causing the job to execute twice.
Focus your tests on the rules users run into every day, including successful requests, invalid input, denied actions, and important edge cases. Laravel distinguishes unit tests, which test a small piece of code without booting the full application, from feature tests, which exercise interactions across components or run a full HTTP request. Laravel’s testing documentation recommends feature tests for most application behavior.
When your tests touch the database, use model factories to build repeatable records and the RefreshDatabase trait to start each test from a known state. When the schema is already up to date, Laravel wraps each test in a database transaction with this trait, which runs faster than rerunning migrations or truncating tables. See the Laravel database testing documentation for setup options. You can also run Laravel Pint locally and in continuous integration to check code style alongside your tests.
Run Laravel’s optimization command during deployment to cache your configuration, events, routes, and views. Those caches cut down the work Laravel has to do on every incoming request. Caching routes helps most when your application defines many of them, though you should still measure response times instead of assuming caching solved a bottleneck. Laravel covers this in its deployment documentation.
Configuration caching changes how Laravel reads environment variables. Once Laravel caches the configuration, it stops loading the .env file on each request, so calls to env() outside your configuration files can return null. Read settings through Laravel’s configuration system in your application code instead.
Use data caching when you know what work it skips and how you will refresh stale entries. Be careful with Cache::flush(), because it deletes every entry in the store and ignores your configured cache prefix. That matters if multiple applications share the same cache store. Laravel’s cache documentation explains the available stores and this flush behavior.
Add rate limits to endpoints that need protection from heavy traffic, such as sensitive or resource-intensive actions. Laravel can key those limits by an authenticated user or an IP address, and it returns HTTP 429 when a client crosses the threshold. Choose limits that fit your users and traffic patterns instead of copying a documentation example as a blanket rule. See the Laravel routing documentation for details.
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