Skip to content
Back to skills

Laravel Eloquent Patterns

ASecurity

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

  • 3 stars
  • 0 votes
  • 0 copies
  • 4 views
  • Added November 15, 2026
ai-agentsphp

Security analysis

A100/100

Scanned November 15, 2026

npx -y skills add workspace-templates/ai-agent-templates --skill laravel-eloquent-patterns --agent claude-code

Installs into .claude/skills of the current project.

Are you the author of Laravel Eloquent Patterns?

Add the live security badge to your README. It updates with every re-scan.

Security grade badge for Laravel Eloquent Patterns
[![Security: A — Skills Directory](https://www.skillsdirectory.com/api/skills/workspace-templates-laravel-eloquent-patterns/badge)](https://www.skillsdirectory.com/skills/workspace-templates-laravel-eloquent-patterns)

More formats (shields.io, HTML) on the badges page. Keep it an A: scan every change in CI with Pro.

Download with Pro
SKILL.md
---
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.

Attribution

Is this your skill, or is something wrong with this listing? Request removal or report an issue. Author removals are honored within 72 hours.

View all in ai-agents →

Comments

Loading comments…

Newsletter

Get the weekly list of safe new skills, and the ones we pulled.