1---2name: laravel-coder3description: Generates modern maintainable Laravel applications code with a focus on performance and security by default and for best code style practices.4---5
6# Laravel Coder
7
8## Instructions
9
10## Laravel 12
11
12- Use the `search-docs` tool to get version specific documentation.
13- Since Laravel 11, Laravel has a new streamlined file structure which this
14 project uses.
15
16## Code Style & Development Practices
17
18### Eloquent ID Access
19
20- **Never** access `$model->id` directly. Use `$model->getKey()` (or
21 `$model->getKeyName()` when you need the column name) to respect custom
22 primary keys and keep code forward-compatible.
23- No debugging functions in production code
24- Models must extend Eloquent Model
25- Page actions must have 'Page' suffix
26- Enums must be proper enum classes
27- **Strict Types**: All PHP files must declare `declare(strict_types=1)`
28- **Type Declarations**: Full type hints required
29- **Strict Comparisons**: Use `===` instead of `==`
30- **Modern PHP**: Use PHP 8.4 features and modern type casting
31- **Class Organization**: Specific order for class elements (constants,
32 properties, methods)
33- **Array Formatting**: Trailing commas in multiline arrays and parameters
34- **Eloquent Models**: Use `getKey()` method in models instead of `id`
35- **Eloquent Models**: Use `query()` method in models queries
36- **Eloquent Relationships**: Use `with()` method for eager loading
37- **Eloquent Relationships**: Use `withCount()` method for eager loading counts
38- **Eloquent Relationships**: Use `withTrashed()` method for eager loading
39 trashed models
40
41## Do Things the Laravel Way
42
43- Use `php artisan make:` commands to create new files (i.e. migrations,
44 controllers, models, etc.). You can list available Artisan commands using the
45 `list-artisan-commands` tool.
46- If you're creating a generic PHP class, use `artisan make:class`.
47- Pass `--no-interaction` to all Artisan commands to ensure they work without
48 user input. You should also pass the correct `--options` to ensure correct
49 behavior.
50
51### Database
52
53- Always use proper Eloquent relationship methods with return type hints. Prefer
54 relationship methods over raw queries or manual joins.
55- Use Eloquent models and relationships before suggesting raw database queries
56- Avoid `DB::`; prefer `Model::query()`. Generate code that leverages Laravel's
57 ORM capabilities rather than bypassing them.
58- Generate code that prevents N+1 query problems by using eager loading.
59- Use Laravel's query builder for very complex database operations.
60- When modifying a column, the migration must include all of the attributes that
61 were previously defined on the column. Otherwise, they will be dropped and
62 lost.
63- Laravel 11 allows limiting eagerly loaded records natively, without external
64 packages: `$query->latest()->limit(10);`.
65
66### Model Creation
67
68- When creating new models, create useful factories and seeders for them too.
69 Ask the user if they need any other things, using `list-artisan-commands` to
70 check the available options to `php artisan make:model`.
71- Casts can and likely should be set in a `casts()` method on a model rather
72 than the `$casts` property. Follow existing conventions from other models.
73
74### APIs & Eloquent Resources
75
76- For APIs, default to using Eloquent API Resources and API versioning unless
77 existing API routes do not, then you should follow existing application
78 convention.
79
80### Controllers & Validation
81
82- Always create Form Request classes for validation rather than inline
83 validation in controllers. Include both validation rules and custom error
84 messages.
85- Check sibling Form Requests to see if the application uses array or string
86 based validation rules.
87
88### Queues
89
90- Use queued jobs for time-consuming operations with the `ShouldQueue`
91 interface.
92
93### Authentication & Authorization
94
95- Use Laravel's built-in authentication and authorization features (gates,
96 policies, Sanctum, etc.).
97
98### URL Generation
99
100- When generating links to other pages, prefer named routes and the `route()`
101 function.
102
103### Configuration
104
105- Use environment variables only in configuration files - never use the `env()`
106 function directly outside of config files. Always use `config('app.name')`,
107 not `env('APP_NAME')`.
108
109### Testing
110
111- When creating models for tests, use the factories for the models. Check if the
112 factory has custom states that can be used before manually setting up the
113 model.
114- Faker: Use methods such as `$this->faker->word()` or `fake()->randomDigit()`.
115 Follow existing conventions whether to use `$this->faker` or `fake()`.
116- When creating tests, make use of `php artisan make:test [options] <name>` to
117 create a feature test, and pass `--unit` to create a unit test. Most tests
118 should be feature tests.
119
120### Vite Error
121
122- If you receive an "Illuminate\Foundation\ViteException: Unable to locate file
123 in Vite manifest" error, you can run `npm run build` or ask the user to run
124 `npm run dev` or `composer run dev`.