---
description: This rule explains Laravel conventions and best practices for backend development.
globs: **/*.php
alwaysApply: false
---
# Laravel rules
- Use `php artisan make:{option}` to create models, migrations, controllers, etc.
- `app\Console\Kernel.php` does not exist in Laravel 11+. If the file is not present, use the the [app.php](mdc:bootstrap/app.php) file for related configurations.
- In Laravel 11+ commands created in `app\Console\Commands\` are automatically registered and available to use.
- Add environment variables to config files and avoid using env variables directly in the code. For example `config('app.name')` instead of `env('APP_NAME')`.
- Avoid N+1 queries by using eager loading or batch loading. Examples:
```php
$users = User::with('posts')->get();
$posts = Post::whereIn('user_id', $users->pluck('id'))->get();
```
- Prefer soft deletes for models using the `SoftDeletes` trait:
```php
class User extends Model
{
use SoftDeletes;
}
```
The Laravel rule in Cursor provides a convenient reference for Laravel conventions and best practices, helping developers maintain consistent, efficient PHP code in Laravel applications. It covers essential Laravel practices like artisan commands, environment variables, database query optimization, and soft delete implementation.
The Laravel rule provides context to Cursor's AI assistant about Laravel framework conventions and best practices for backend development. When active, it guides code suggestions and completions to align with Laravel's preferred patterns and approaches. This rule helps developers avoid common pitfalls and follow recommended Laravel methodologies for:
The rule encourages using Laravel's built-in artisan command tool for creating framework components. Instead of manually creating files, you should use commands like:
php artisan make:model Post
php artisan make:controller PostController
php artisan make:migration create_posts_table
The rule recommends proper environment variable usage by accessing them through config files rather than directly with the env()
helper function in your application code:
// Recommended
$appName = config('app.name');
// Not recommended
$appName = env('APP_NAME');
This practice ensures your application uses cached configuration values in production environments.
To prevent the N+1 query problem (a common performance issue), the rule suggests using eager loading with the with()
method or batch loading strategies:
// Eager loading example
$users = User::with('posts')->get();
// Batch loading example
$posts = Post::whereIn('user_id', $users->pluck('id'))->get();
For models that shouldn't be permanently removed from the database, the rule advises implementing soft deletes:
class User extends Model
{
use SoftDeletes;
}
The rule provides guidance specific to Laravel 11+, noting that:
app\Console\Kernel.php
file may not existapp\Console\Commands\
are automatically registeredThe Laravel rule is stored in your project's .cursor/rules
directory as laravel.mdc
. This rule is configured as an "Auto Attached" rule with the glob pattern **/*.php
, meaning it automatically activates whenever you're working with PHP files in your project.
When active, the rule provides contextual guidance to Cursor's AI, influencing:
You don't need to manually activate this rule - it will automatically provide Laravel-specific context when you're editing PHP files. If you want to explicitly reference it in a conversation with the AI assistant, you can type @laravel
in the chat or Cmd-K interface.