1---2name: laravel3description: How to work effectively with Laravel, always use when developing Laravel features4---56# Laravel78## Instructions910## Do Things the Laravel Way1112- Use `php artisan make:` commands to create new files (i.e. migrations, controllers, models, etc.).13- If you're creating a generic PHP class, use `php artisan make:class`.14- Pass `--no-interaction` to all Artisan commands to ensure they work without user input. You should also pass the correct `--options` to ensure correct behavior.1516### Database1718- Always use proper Eloquent relationship methods with return type hints. Prefer relationship methods over raw queries or manual joins.19- Use Eloquent models and relationships before suggesting raw database queries20- Avoid `DB::`; prefer `Model::query()`. Generate code that leverages Laravel's ORM capabilities rather than bypassing them.21- Generate code that prevents N+1 query problems by using eager loading.22- Use Laravel's query builder for very complex database operations.2324### Model Creation2526- When creating new models, create useful factories and seeders for them too. Ask the user if they need any other things.2728### APIs & Eloquent Resources2930- For APIs, default to using Eloquent API Resources and API versioning unless existing API routes do not, then you should follow existing application convention.3132### Controllers & Validation3334- Always create Form Request classes for validation rather than inline validation in controllers. Include both validation rules and custom error messages.35- Check sibling Form Requests to see if the application uses array or string based validation rules.3637### Queues3839- Use queued jobs for time-consuming operations with the `ShouldQueue` interface.4041### Authentication & Authorization4243- Use Laravel's built-in authentication and authorization features (gates, policies, Sanctum, etc.).4445### URL Generation4647- When generating links to other pages, prefer named routes and the `route()` function.4849### Configuration5051- Use environment variables only in configuration files - never use the `env()` function directly outside of config files. Always use `config('app.name')`, not `env('APP_NAME')`.5253### Testing5455- When creating models for tests, use the factories for the models. Check if the factory has custom states that can be used before manually setting up the model.56- Faker: Use methods such as `$this->faker->word()` or `fake()->randomDigit()`. Follow existing conventions whether to use `$this->faker` or `fake()`.57- When creating tests, make use of `php artisan make:test [options] {name}` to create a feature test, and pass `--unit` to create a unit test. Most tests should be feature tests.5859## Laravel 126061- Since Laravel 11, Laravel has a new streamlined file structure which this project uses.6263### Laravel 12 Structure6465- No middleware files in `app/Http/Middleware/`.66- `bootstrap/app.php` is the file to register middleware, exceptions, and routing files.67- `bootstrap/providers.php` contains application specific service providers.68- **No app\Console\Kernel.php** - use `bootstrap/app.php` or `routes/console.php` for console configuration.69- **Commands auto-register** - files in `app/Console/Commands/` are automatically available and do not require manual registration.7071### Database7273- When modifying a column, the migration must include all of the attributes that were previously defined on the column. Otherwise, they will be dropped and lost.74- Laravel 11 allows limiting eagerly loaded records natively, without external packages: `$query->latest()->limit(10);`.7576### Models7778- Casts can and likely should be set in a `casts()` method on a model rather than the `$casts` property. Follow existing conventions from other models.