Laravel Best Practices logo

Laravel Best Practices

Community
gdarko
laravel-best-practices

Apply this skill whenever writing, reviewing, or refactoring Laravel PHP code. This includes creating or modifying controllers, models, migrations, form requests, policies, jobs, scheduled commands, service classes, and Eloquent queries. Triggers for N+1 and query performance issues, caching strategies, authorization and security patterns, validation, error handling, queue and job configuration, route definitions, and architectural decisions. Also use for Laravel code reviews and refactoring existing Laravel code to follow best practices. Covers any task involving Laravel backend PHP code patterns.

Overview

Publishergdarko
Repositorylaravel-vue-starter
Skill namelaravel-best-practices
Stars
145
Forks
61
Bundled files
20
LicenseMIT
Links
  • Markdown instructions

    A SKILL.md file the model loads on demand, so it only costs tokens when a request actually matches.

  • Works with any LLM

    AI skills are plain Markdown, not provider-specific code, so this works with GPT, Claude, Gemini, Grok, or a local model.

  • 20 bundled files

    Scripts, templates, and references the model can read while it works. Files are read-only and never executed.

  • Open source

    Published by gdarko on GitHub. Read the source before you install it.

Installation

Install the Laravel Best Practices AI skill in TypingMind to use it with any LLM, or drop it into another agent that reads SKILL.md.

1

Install in TypingMind

TypingMind installs a skill straight from its GitHub folder — it reads SKILL.md, bundles the resource files, and stores the result locally.

  1. Open the app and go to Plugins → Skills.
  2. Choose "Install from GitHub".
  3. Paste the skill folder URL below and confirm.
    https://github.com/gdarko/laravel-vue-starter/tree/main/.agents/skills/laravel-best-practices
  4. Enable the skill in any chat where you want it available.
Plugins → Skills → Add skill → From GitHub URL, then paste the folder URL and press Continue.
2

Install in another agent

Any agent that reads the Agent Skills format can use this skill — copy the folder into that agent's skills directory.

Claude Code — .claude/skills
git clone --depth 1 https://github.com/gdarko/laravel-vue-starter.git /tmp/laravel-vue-starter
mkdir -p .claude/skills
cp -r /tmp/laravel-vue-starter/.agents/skills/laravel-best-practices .claude/skills/laravel-best-practices
Restart Claude Code after copying so it picks up the new skill.

Use it in TypingMind

Enable Laravel Best Practices in any TypingMind chat and the model takes it from there. Its name and description sit in the system prompt, and the moment a request matches, the model loads the full instructions itself — you never invoke it by hand, and it costs no tokens until it is actually used.

The model loads Laravel Best Practices on its own as soon as a request matches it.

Works with any AI model

AI skills are plain Markdown instructions rather than provider-specific code, so Laravel Best Practices is not tied to the model it was written for. Install it once in TypingMind and use it with GPT-5, Claude, Gemini, Grok, DeepSeek, Mistral, Llama, or a local model you run yourself — all on your own API keys.

  • Loaded only when it is needed

    The system prompt carries just the name and description. The instructions are fetched on the first matching request, so an idle skill costs nothing.

  • Switch models mid-chat

    Because the skill is instructions rather than code, changing model does not break it — the next model reads the same SKILL.md.

Skill instructions

This is the SKILL.md content the model loads. Read it before installing — a skill is instructions your model will follow.

Laravel Best Practices

Best practices for Laravel, prioritized by impact. Each rule teaches what to do and why. For exact API syntax, verify with search-docs.

Consistency First

Before applying any rule, check what the application already does. Laravel offers multiple valid approaches — the best choice is the one the codebase already uses, even if another pattern would be theoretically better. Inconsistency is worse than a suboptimal pattern.

Check sibling files, related controllers, models, or tests for established patterns. If one exists, follow it — don't introduce a second way. These rules are defaults for when no pattern exists yet, not overrides.

Quick Reference

1. Database Performance → rules/db-performance.md

  • Eager load with with() to prevent N+1 queries
  • Enable Model::preventLazyLoading() in development
  • Select only needed columns, avoid SELECT *
  • chunk() / chunkById() for large datasets
  • Index columns used in WHERE, ORDER BY, JOIN
  • withCount() instead of loading relations to count
  • cursor() for memory-efficient read-only iteration
  • Never query in Blade templates

2. Advanced Query Patterns → rules/advanced-queries.md

  • addSelect() subqueries over eager-loading entire has-many for a single value
  • Dynamic relationships via subquery FK + belongsTo
  • Conditional aggregates (CASE WHEN in selectRaw) over multiple count queries
  • setRelation() to prevent circular N+1 queries
  • whereIn + pluck() over whereHas for better index usage
  • Two simple queries can beat one complex query
  • Compound indexes matching orderBy column order
  • Correlated subqueries in orderBy for has-many sorting (avoid joins)

3. Security → rules/security.md

  • Define $fillable or $guarded on every model, authorize every action via policies or gates
  • No raw SQL with user input — use Eloquent or query builder
  • {{ }} for output escaping, @csrf on all POST/PUT/DELETE forms, throttle on auth and API routes
  • Validate MIME type, extension, and size for file uploads
  • Never commit .env, use config() for secrets, encrypted cast for sensitive DB fields

4. Caching → rules/caching.md

  • Cache::remember() over manual get/put
  • Cache::flexible() for stale-while-revalidate on high-traffic data
  • Cache::memo() to avoid redundant cache hits within a request
  • Cache tags to invalidate related groups
  • Cache::add() for atomic conditional writes
  • once() to memoize per-request or per-object lifetime
  • Cache::lock() / lockForUpdate() for race conditions
  • Failover cache stores in production

5. Eloquent Patterns → rules/eloquent.md

  • Correct relationship types with return type hints
  • Local scopes for reusable query constraints
  • Global scopes sparingly — document their existence
  • Attribute casts in the casts() method
  • Cast date columns, use Carbon instances in templates
  • whereBelongsTo($model) for cleaner queries
  • Never hardcode table names — use (new Model)->getTable() or Eloquent queries

6. Validation & Forms → rules/validation.md

  • Form Request classes, not inline validation
  • Array notation ['required', 'email'] for new code; follow existing convention
  • $request->validated() only — never $request->all()
  • Rule::when() for conditional validation
  • after() instead of withValidator()

7. Configuration → rules/config.md

  • env() only inside config files
  • App::environment() or app()->isProduction()
  • Config, lang files, and constants over hardcoded text

8. Testing Patterns → rules/testing.md

  • LazilyRefreshDatabase over RefreshDatabase for speed
  • assertModelExists() over raw assertDatabaseHas()
  • Factory states and sequences over manual overrides
  • Use fakes (Event::fake(), Exceptions::fake(), etc.) — but always after factory setup, not before
  • recycle() to share relationship instances across factories

9. Queue & Job Patterns → rules/queue-jobs.md

  • retry_after must exceed job timeout; use exponential backoff [1, 5, 10]
  • ShouldBeUnique to prevent duplicates; WithoutOverlapping::untilProcessing() for concurrency
  • Always implement failed(); with retryUntil(), set $tries = 0
  • RateLimited middleware for external API calls; Bus::batch() for related jobs
  • Horizon for complex multi-queue scenarios

10. Routing & Controllers → rules/routing.md

  • Implicit route model binding
  • Scoped bindings for nested resources
  • Route::resource() or apiResource()
  • Methods under 10 lines — extract to actions/services
  • Type-hint Form Requests for auto-validation

11. HTTP Client → rules/http-client.md

  • Explicit timeout and connectTimeout on every request
  • retry() with exponential backoff for external APIs
  • Check response status or use throw()
  • Http::pool() for concurrent independent requests
  • Http::fake() and preventStrayRequests() in tests

12. Events, Notifications & Mail → rules/events-notifications.md, rules/mail.md

  • Event discovery over manual registration; event:cache in production
  • ShouldDispatchAfterCommit / afterCommit() inside transactions
  • Queue notifications and mailables with ShouldQueue
  • On-demand notifications for non-user recipients
  • HasLocalePreference on notifiable models
  • assertQueued() not assertSent() for queued mailables
  • Markdown mailables for transactional emails

13. Error Handling → rules/error-handling.md

  • report()/render() on exception classes or in bootstrap/app.php — follow existing pattern
  • ShouldntReport for exceptions that should never log
  • Throttle high-volume exceptions to protect log sinks
  • dontReportDuplicates() for multi-catch scenarios
  • Force JSON rendering for API routes
  • Structured context via context() on exception classes

14. Task Scheduling → rules/scheduling.md

  • withoutOverlapping() on variable-duration tasks
  • onOneServer() on multi-server deployments
  • runInBackground() for concurrent long tasks
  • environments() to restrict to appropriate environments
  • takeUntilTimeout() for time-bounded processing
  • Schedule groups for shared configuration

15. Architecture → rules/architecture.md

  • Single-purpose Action classes; dependency injection over app() helper
  • Prefer official Laravel packages and follow conventions, don't override defaults
  • Default to ORDER BY id DESC or created_at DESC; mb_* for UTF-8 safety
  • defer() for post-response work; Context for request-scoped data; Concurrency::run() for parallel execution

16. Migrations → rules/migrations.md

  • Generate migrations with php artisan make:migration
  • constrained() for foreign keys
  • Never modify migrations that have run in production
  • Add indexes in the migration, not as an afterthought
  • Mirror column defaults in model $attributes
  • Reversible down() by default; forward-fix migrations for intentionally irreversible changes
  • One concern per migration — never mix DDL and DML

17. Collections → rules/collections.md

  • Higher-order messages for simple collection operations
  • cursor() vs. lazy() — choose based on relationship needs
  • lazyById() when updating records while iterating
  • toQuery() for bulk operations on collections

18. Blade & Views → rules/blade-views.md

  • $attributes->merge() in component templates
  • Blade components over @include; @pushOnce for per-component scripts
  • View Composers for shared view data
  • @aware for deeply nested component props

19. Conventions & Style → rules/style.md

  • Follow Laravel naming conventions for all entities
  • Prefer Laravel helpers (Str, Arr, Number, Uri, Str::of(), $request->string()) over raw PHP functions
  • No JS/CSS in Blade, no HTML in PHP classes
  • Code should be readable; comments only for config files

How to Apply

Always use a sub-agent to read rule files and explore this skill's content.

  1. Identify the file type and select relevant sections (e.g., migration → §16, controller → §1, §3, §5, §6, §10)
  2. Check sibling files for existing patterns — follow those first per Consistency First
  3. Verify API syntax with search-docs for the installed Laravel version

Bundled files

The model reads these on demand while the skill is loaded. They are exposed as readable files and are never executed.

Frequently asked questions

What does the Laravel Best Practices AI skill do?

Apply this skill whenever writing, reviewing, or refactoring Laravel PHP code. This includes creating or modifying controllers, models, migrations, form requests, policies, jobs, scheduled commands, service classes, and Eloquent queries. Triggers for N+1 and query performance issues, caching strategies, authorization and security patterns, validation, error handling, queue and job configuration, route definitions, and architectural decisions. Also use for Laravel code reviews and refactoring existing Laravel code to follow best practices. Covers any task involving Laravel backend PHP code p...

Why use Laravel Best Practices on TypingMind?

Because you install it once and use it with any model. Laravel Best Practices is plain Markdown rather than provider-specific code, so the same skill runs on GPT-5, Claude, Gemini, Grok, or a local model — and you can switch model mid-chat without it breaking. TypingMind runs on your own API keys, so you pay providers directly instead of a per-seat subscription, and your skills and chats stay in your own storage.

How do I install Laravel Best Practices in TypingMind?

Open Plugins → Skills → Install from GitHub in TypingMind and paste https://github.com/gdarko/laravel-vue-starter/tree/main/.agents/skills/laravel-best-practices. TypingMind reads its SKILL.md and bundles its files and installs it as a skill you can enable per chat.

Which AI models can use Laravel Best Practices?

Any model you connect in TypingMind. AI skills are plain Markdown instructions rather than provider-specific code, so GPT, Claude, Gemini, Grok, and local models can all load this skill when a request matches it.

How many AI models can I use with Laravel Best Practices?

As many as you like. As long as a model supports skills, you can use Laravel Best Practices with it — GPT, Claude, Gemini, Grok, DeepSeek, Mistral, Llama and more — all on TypingMind with your own API keys.

Is the Laravel Best Practices AI skill free?

Yes. It is published on GitHub by gdarko under the MIT license. You only pay your own AI provider for the tokens you use.

What are AI skills?

An AI skill is a reusable instruction bundle that teaches an AI model how to do one specific task. It follows the open Agent Skills format: a SKILL.md file with a name and description, plus any scripts, templates or reference files the model may need. The model reads the instructions only when your request matches the skill, so an installed skill costs nothing until it is used.

How are AI skills different from plugins or MCP servers?

A plugin or MCP server gives a model new tools to call — code that runs somewhere and returns a result. An AI skill gives the model knowledge and process instead: how to approach a task, which steps to follow, what good output looks like. Skills are plain Markdown, so they need no server, no API key and no runtime, and they work with any model.

View all

Set up your own AI workspace now

Get notified about new features and future giveaways by subscribing to our newsletter 👇