{"dataset":{"id":"00000000-0000-0000-0000-000000000050","slug":"laravel-best-practices","name":"Laravel best practices","description":"Source-linked guidance on Laravel fundamentals: routing, Eloquent, migrations, Blade templates, and middleware. Scoped to current Laravel; not a PHP language corpus.","visibility":"public","version":1,"documentCount":5,"publisher":"Koskamo editorial","license":"Original summaries; linked sources retain their own terms.","reviewedAt":"2026-10-04T00:00:00.000Z","readOnly":true,"createdAt":"2026-10-04T00:00:00.000Z","updatedAt":"2026-10-04T00:00:00.000Z"},"total":5,"documents":[{"title":"Middleware","sourceUrl":"https://laravel.com/docs/12.x/middleware","text":"Middleware provide a convenient mechanism for inspecting and filtering HTTP requests entering an application; Laravel includes middleware for authentication and CSRF protection. The authentication middleware redirects unauthenticated users to the login screen but allows authenticated requests to proceed further into the application. Create middleware with the make:middleware Artisan command, which places the class in app/Http/Middleware. Each middleware defines a handle method receiving a Request and a Closure and returning a Response. The docs' EnsureTokenIsValid example allows access to the route only if the supplied token input matches 'my-secret-token'; otherwise it redirects to /home, while a match returns $next($request) to pass the request further. Middleware can perform tasks besides authentication, such as logging all incoming requests. Scope: Laravel. Reviewed October 4, 2026.","id":"laravel-best-practices:Middleware","version":1,"createdAt":"2026-10-04T00:00:00.000Z","updatedAt":"2026-10-04T00:00:00.000Z"},{"title":"Blade templates","sourceUrl":"https://laravel.com/docs/12.x/blade","text":"Blade is the simple templating engine included with Laravel; all Blade templates are compiled into plain PHP code and cached until modified, adding essentially zero overhead. Templates use the .blade.php extension, typically live in resources/views, and do not restrict plain PHP. Return views from routes or controllers with the global view helper, passing data via its second argument; {{ }} echo statements are automatically sent through PHP's htmlspecialchars function to prevent XSS attacks. Blade directives such as @if, @foreach, and @while provide conditional and loop logic, while components, slots, and layouts organize interfaces. For dynamic interfaces, Livewire augments Blade components with functionality typically requiring frontend frameworks like React, Svelte, or Vue, without those frameworks' complexities, client-side rendering, or build steps. Scope: Laravel. Reviewed October 4, 2026.","id":"laravel-best-practices:Blade templates","version":1,"createdAt":"2026-10-04T00:00:00.000Z","updatedAt":"2026-10-04T00:00:00.000Z"},{"title":"Database migrations","sourceUrl":"https://laravel.com/docs/12.x/migrations","text":"Migrations are like version control for your database, letting teams define and share schema definitions. The Laravel Schema facade provides database-agnostic creation and manipulation of tables and columns across all supported database systems. Generate migrations with the make:migration Artisan command; files land in database/migrations, and each filename contains a timestamp determining execution order. Laravel attempts to guess the table name and creation intent from the migration name, pre-filling the file when successful; otherwise specify the table manually. Use --path for a custom output path relative to the base path, and customize stubs via stub publishing. To slim an accumulating database/migrations directory, squash migrations into a single SQL file with schema:dump; add --prune to also prune all existing migrations. Scope: Laravel. Reviewed October 4, 2026.","id":"laravel-best-practices:Database migrations","version":1,"createdAt":"2026-10-04T00:00:00.000Z","updatedAt":"2026-10-04T00:00:00.000Z"},{"title":"Eloquent models","sourceUrl":"https://laravel.com/docs/12.x/eloquent","text":"Laravel includes Eloquent, an object-relational mapper (ORM) where each database table has a corresponding Model class used to interact with that table. Configure a database connection in config/database.php before getting started. Models typically live in the app\\Models directory and extend Illuminate\\Database\\Eloquent\\Model; generate them with php artisan make:model, optionally creating related files via options like --migration or --factory. Besides retrieving records, models can insert, update, and delete; the upsert method updates or creates records in a single atomic operation. Eloquent supports UUID and ULID primary keys and automatically manages timestamps. Chunking, lazy collections, and cursors process large result sets efficiently; global and local scopes reuse query constraints. Soft deletes, pruning, replication, events, observers, and strictness options are also available. Scope: Laravel. Reviewed October 4, 2026.","id":"laravel-best-practices:Eloquent models","version":1,"createdAt":"2026-10-04T00:00:00.000Z","updatedAt":"2026-10-04T00:00:00.000Z"},{"title":"Routing and route parameters","sourceUrl":"https://laravel.com/docs/12.x/routing","text":"Define web routes in routes/web.php, which automatically assigns the web middleware group for session state and CSRF protection. For stateless APIs, enable routing via the install:api Artisan command, which installs Laravel Sanctum for API token authentication and creates the routes/api.php file. Routes in routes/api.php receive the api middleware group and the /api URI prefix automatically. Configure route file loading in bootstrap/app.php. Basic routes accept a URI and closure or controller action, such as Route::get('/user', [UserController::class, 'index']). The install:api command ensures robust third-party API consumer authentication. Omit auth:sanctum middleware for publicly accessible API routes. Modify the api prefix in bootstrap/app.php if needed. Laravel automatically loads route files from the routes directory using the configuration in bootstrap/app.php. Use Route::get for HTTP GET requests. Controllers can be specified using array syntax with class name and method. This structure separates web interface routes from API endpoints effectively. Scope: Laravel. Reviewed October 4, 2026.","id":"laravel-best-practices:Routing and route parameters","version":1,"createdAt":"2026-10-04T00:00:00.000Z","updatedAt":"2026-10-04T00:00:00.000Z"}]}