Skip to content

PHP and Laravel

Scope: web applications and APIs built with Laravel and PHP, with Eloquent, Blade, queues and Horizon. On 2026-11-01, Laravel 13 is the current release: it needs PHP 8.3 or later, gets bug fixes until the third quarter of 2027 and security fixes until 17 March 2028. Laravel 12 gets security fixes only, until 24 February 2027. For configuration, health checks, telemetry and resilience, follow general/operations-reliability.md; for secrets, authorization and the supply chain, general/security.md; for sign-in and sessions, general/authentication.md; for migrations and idempotent message handling, general/data.md. Browser-side rules for accessibility, performance and security are in Web front end; databases, caches and containers have their own folders, such as PostgreSQL, Redis and caching and Docker. The rules here cover what is specific to Laravel.

Entries are sorted by score from 1 to 10 (criteria): 9โ€“10 start here or strongly recommended, 6โ€“8 recommended, 1โ€“5 further reading or conditional.

Adopted references

  • 9 Laravel documentation (Laravel): the baseline. Configuration, routing, requests and validation, Blade, authentication and authorization, CSRF protection, Eloquent and migrations, queues, file storage, testing and deployment.
  • 8 Deployment (Laravel docs): server requirements, a web server configuration, the caches to build during deployment, debug mode, the health route, and reloading long-running processes.
  • 7 Release notes: support policy and upgrade guide (Laravel docs): the bug-fix and security-fix dates and PHP versions of each Laravel release, and the breaking changes to handle when you move to the next one.
  • 7 Supported Versions (PHP): the active-support and security-support end dates of each PHP branch.
  • 6 Laravel Cheat Sheet (OWASP): Laravel security settings and pitfalls, such as cookies and sessions, mass assignment, SQL injection, XSS, file uploads, CSRF and rate limiting.
  • 6 Horizon (Laravel docs): a dashboard and code-driven worker configuration for queues that run on Redis, with metrics, tags and failed-job management. Use it when your queues run on Redis.
  • 6 PER Coding Style (PHP-FIG): the PHP coding style that extends and replaces PSR-12.

Key rules

Configuration and deployment

  • 9 LARAVEL-1 Never commit the .env file: commit .env.example with placeholder values, and supply the real values as environment variables on the server or in an encrypted environment file (php artisan env:encrypt). Generate APP_KEY with php artisan key:generate; when you rotate it, list the old keys in APP_PREVIOUS_KEYS, so that existing sessions and encrypted data keep working. This is how Laravel applications follow SEC-2 in general/security.md. (Laravel, Environment file security, Encrypting environment files, Encryption: configuration, Gracefully rotating encryption keys)
  • 9 LARAVEL-2 In production, set APP_DEBUG to false and point the web server's document root at the public directory. With debug mode on, error pages can expose sensitive configuration values to users, and serving the project root exposes sensitive configuration files to the internet. (Laravel, Deployment: debug mode, Deployment: Nginx)
  • 7 LARAVEL-3 Run php artisan optimize in every deployment, to cache the configuration, events, routes and views; call env() only in the files in config/; and restart queue workers and other long-running processes after deploying, for example with php artisan reload. Once the configuration is cached, Laravel no longer loads .env, so env() calls elsewhere return null for values that are defined only in .env. (Laravel, Deployment: optimization, Configuration caching, Deployment: reloading services)

Security

  • 9 LARAVEL-4 Authorize every action: write the rules for each model in a policy and the other rules in gates, and check them in each controller action, in the Form Request's authorize() method, or with the can middleware. Gates and policies deny guests by default. This is how Laravel applications apply SEC-1 in general/security.md. (Laravel, Authorization, Authorizing actions via middleware, Guest users, Authorizing form requests)
  • 9 LARAVEL-5 Output data with Blade's escaped {{ }} syntax, use {!! !!} only for HTML you trust, never for content that users supplied, and pass data to JavaScript with Js::from(). {{ }} runs values through htmlspecialchars to prevent XSS. This is how Blade templates apply WEB-8; for sanitizing HTML and Content Security Policy, see Web front end. (Laravel, Blade: displaying unescaped data, Rendering JSON)
  • 9 LARAVEL-6 Pass values to raw SQL only as bindings, with ? or named placeholders, in DB::select(), whereRaw(), orderByRaw() and the other raw methods, and never let user input choose column names, including orderBy columns. The query builder binds values for you, but raw expressions are inserted into the query as strings, PDO can't bind column names, and DB::unprepared() binds nothing. This applies SEC-18. (Laravel, Query builder, Raw methods, Database: running a select query, Running an unprepared statement)
  • 8 LARAVEL-7 Validate every request with a Form Request or the validator, pass only the validated data ($request->validated() or safe()) to create(), fill() and update(), and declare each model's mass-assignable attributes with the Fillable attribute (the $fillable property before Laravel 13) instead of unguarding the model. Otherwise a user can send an extra field, such as is_admin, that changes a column you didn't expect. In development, Model::preventSilentlyDiscardingAttributes() throws when code fills an attribute that isn't fillable. This applies SEC-10 in general/security.md. (Laravel, Form request validation, Working with validated input, Eloquent: mass assignment, Allowing mass assignment, Mass assignment exceptions)
  • 7 LARAVEL-8 Validate the type and size of every upload with the File rule, for example File::types([...])->max('10mb') or File::image(), and store it under a generated name with store() or hashName(), never a name built from getClientOriginalName(), getClientOriginalExtension() or other user input. An attacker could upload a file named shell.php and run it if the storage is web-accessible. File::types() checks the file's content, not only its extension, and the image rule rejects SVG files unless you allow them. (Laravel, Validating files, Validating file types, File storage: file uploads, Specifying a file name, Other uploaded file information)
  • 7 LARAVEL-9 Keep browser routes in the web middleware group, where the PreventRequestForgery middleware checks them, add @csrf to every POST, PUT, PATCH and DELETE form, and exclude only routes that can't send a token, such as third-party webhooks, from CSRF protection. For a single-page app that uses Laravel as its API, follow the Sanctum documentation. This applies AUTH-17. (Laravel, Preventing CSRF requests, Excluding URIs from CSRF protection)

Database

  • 8 LARAVEL-10 Change the schema only through migrations in database/migrations, run them in each deployment with php artisan migrate --force, and add --isolated when several servers deploy at once. --isolated takes a lock, so that only one server runs the migrations. This is how Laravel applications follow DATA-2 in general/data.md. (Laravel, Migrations, Isolating migration execution, Forcing migrations to run in production)
  • 7 LARAVEL-11 Eager load the relationships that each query's results use with with() or load(), and call Model::preventLazyLoading(! $this->app->isProduction()) in AppServiceProvider, so that lazy loading throws an exception in development and tests. Production keeps working if a lazy load slips through. This is how Eloquent applies DATA-10. (Laravel, Eager loading, Preventing lazy loading)
  • 7 LARAVEL-12 Wrap writes that must succeed or fail together in DB::transaction(), pass attempts to retry on deadlocks, and dispatch queued jobs from inside a transaction only after it commits, with the queue connection's after_commit option or ->afterCommit(). Otherwise a worker can pick up the job before the transaction commits, when the records it needs may not exist yet. (Laravel, Database transactions, Handling deadlocks, Queues: jobs and database transactions)

Queues

  • 7 LARAVEL-13 Run slow work, such as processing uploaded files, in queued jobs; make each job safe to run more than once, keep each worker's --timeout several seconds shorter than the connection's retry_after, limit the attempts, and keep failed jobs in the failed_jobs table to inspect and retry them. If --timeout is longer than retry_after, a job can be processed twice. Idempotent consumers are DATA-9 in general/data.md. (Laravel, Queues, Job expirations and timeouts, Dealing with failed jobs)

Code quality and testing

  • 6 LARAVEL-14 Write most tests as feature tests that send HTTP requests to the application and assert on the response and the database, reset the database for each test with RefreshDatabase, and create test data with model factories. The Laravel docs say most tests should be feature tests. Their requests are simulated inside the test process, without a real network request; Laravel supports both Pest and PHPUnit. (Laravel, Testing, HTTP tests: making requests, Database testing: resetting the database, Model factories)
  • 6 LARAVEL-15 Check the code style with Laravel Pint, and analyse the code with Larastan, the PHPStan extension for Laravel, in CI. Pint uses its laravel preset by default; choose the per or psr12 preset to follow the PHP-FIG style. (Laravel, Pint, Pint presets, Larastan)

Versions

How to enforce

Tools that check the key rules above automatically, in CI, the IDE or a commit hook. "Partly" means the tool covers only some of what the rule asks.

Tutorials and further reading

  • 7 Getting Started with Laravel (Laravel): the official course that builds a first full-stack application from start to finish.
  • 6 Laravel From Scratch, 2026 edition (Laracasts): a free introductory video series, refreshed every two years and covering Laravel 12 and later, from the fundamentals to authentication, authorization, queues, testing and deployment.
  • 5 PHP The Right Way (community): current PHP practices, coding standards, dependency management, security and testing, for any PHP project.