Skip to content

skillBackend and APIsphplaravel

Laravel Eloquent Patterns

Writes Laravel model code the idiomatic way: typed relations, query scopes, form requests and eager loading. Use it when adding or reviewing Eloquent code.

npx workspacetemplates add laravel-eloquent-patterns --tool claude-code

Writes 1 file to .claude/skills/laravel-eloquent-patterns/

.claude/skills/laravel-eloquent-patterns/SKILL.md29 lines · 1.7 KBFile
---
name: laravel-eloquent-patterns
description: "Writes Laravel model code the idiomatic way: typed relations, query scopes, form requests and eager loading. Use it when adding or reviewing Eloquent code."
---

# Laravel Eloquent patterns

Use the framework's own structure before inventing one. Match the Laravel version in composer.json.

## Steps

1. Read the model, its migration and the controller that uses it. Note the foreign keys and casts already there.
2. Declare every relation with a return type (`HasMany`, `BelongsTo` and so on) and keep the foreign key name explicit when it is not the default.
3. Search the code you touch for loops that read a relation inside a `foreach`. Replace the lazy read with `with()` on the query, or `load()` when the model already exists. In development, suggest `Model::preventLazyLoading()` so the next N+1 fails loudly.
4. Move a where clause into a local scope (`scopeActive`) when it appears twice. One-off conditions stay inline.
5. Move validation and authorization into a form request class: `rules()` for input, `authorize()` for the policy check. The controller then calls `$request->validated()` and nothing else.
6. Declare `$fillable` explicitly. Never use `$guarded = []` and never pass `$request->all()` to `create()`.
7. Wrap writes that touch several tables in `DB::transaction()`.

## Rules

- Use `chunkById()` for large jobs, never `all()` on a big table.
- Put counts in `withCount()`, not in a loop of `->count()` calls.
- Do not add a repository layer on top of Eloquent unless the project already has one.
- Run `php artisan test` after each change and report failures first.

## Report

List each file changed, the query count before and after for any page you optimized, and anything you left alone on purpose.

What it does

Laravel makes it easy to put a query in a controller, a validation rule in a closure and an N+1 loop in a Blade view. This skill steers the agent toward the structure the framework already gives you, so models stay small and controllers stay thin.

It checks relations for eager loading, moves repeated where clauses into scopes, and puts input rules in form requests. Raw SQL and mass-assignment shortcuts are flagged instead of copied.

Example

you › The orders index page is slow. Clean up the Eloquent code behind it.

agent › OrderController@index ran 1 + 50 + 50 queries: each row read `$order->customer` and `$order->items`. Added `with(['customer','items'])` and `withCount('items')`, now 3 queries. Moved the repeated `where('status','open')` into `scopeOpen` and validation into `IndexOrdersRequest`. Tests pass. Left the raw SQL report query alone.

Questions

Does it work with older Laravel versions?
It reads composer.json first and avoids features your version lacks, such as typed scope attributes or newer cast syntax.
Will it add a repository pattern?
No. Eloquent already is the data layer, and the skill only suggests extra layers when your project already uses them.
  • API Error Responses

    SkillBackend and APIs

    Free
  • API Pagination Design

    SkillBackend and APIs

    Free
  • API Versioning Strategy

    SkillBackend and APIs

    Free
  • Background Job Design

    SkillBackend and APIs

    Free