Cursor Rules for
Laravel

This rule explains Laravel conventions and best practices for backend development.
Back to rules
Type
Backend
Language(s)
PHP
Stats
838 views
38 copies
27 downloads
Contributors
Venelin Kochev
Venelin Kochev
Venelin Kochev
laravel.mdc
---
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.

What this rule does

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:

  • Using artisan commands
  • Managing environment variables
  • Preventing database query performance issues
  • Implementing soft deletes
  • Working with Laravel 11+ specific features

Laravel conventions and best practices

Artisan commands

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

Configuration management

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.

Database query optimization

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();

Soft deletes

For models that shouldn't be permanently removed from the database, the rule advises implementing soft deletes:

class User extends Model
{
    use SoftDeletes;
}

Laravel 11+ considerations

The rule provides guidance specific to Laravel 11+, noting that:

  • The app\Console\Kernel.php file may not exist
  • Commands in app\Console\Commands\ are automatically registered

Using Laravel in Cursor

The 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:

  • Code completions
  • Recommendations
  • Generated code
  • Responses to your questions about Laravel

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.

Usage tips

  • Use this rule when working on Laravel applications to ensure the AI provides suggestions that follow Laravel best practices
  • Combine this rule with other PHP-related rules for comprehensive guidance
  • Consider expanding the rule with project-specific Laravel conventions if your team has additional standards
  • The rule is particularly helpful for developers new to Laravel who need guidance on framework conventions
Want to 10x your AI skills?

Get a free account and learn to code + market your apps using AI (with or without vibes!).

Nah, maybe later