Skip to content
Back to skills

Laravel Eloquent Conventions

ASecurity

Use when creating or editing Eloquent models, relationships, casts, or accessors in this project - covers this codebase's specific conventions for return types, casts, and relationship style

  • 2 stars
  • 0 votes
  • 0 copies
  • 4 views
  • Added November 28, 2026
databasesrustphpdatabase

Security analysis

A100/100

Scanned November 28, 2026

npx -y skills add mst-software-vn/mst-checkscam --skill laravel-eloquent-conventions --agent claude-code

Installs into .claude/skills of the current project.

Are you the author of Laravel Eloquent Conventions?

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

Security grade badge for Laravel Eloquent Conventions
[![Security: A — Skills Directory](https://www.skillsdirectory.com/api/skills/mst-software-vn-laravel-eloquent-conventions/badge)](https://www.skillsdirectory.com/skills/mst-software-vn-laravel-eloquent-conventions)

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-conventions
description: Use when creating or editing Eloquent models, relationships, casts, or accessors in this project - covers this codebase's specific conventions for return types, casts, and relationship style
---

# Laravel Eloquent Conventions (this project)

## Overview

This codebase has two generations of model style. **Newer models follow stricter conventions matching CLAUDE.md** - follow the newer style for any new code. Only match the older style when editing an old file in place (don't rewrite unrelated methods).

## Newer convention (use this for new models/methods)

Reference: `app/Models/BankDeposit.php`, `app/Models/AdminTrustedDevice.php`

```php
protected function casts(): array
{
    return [
        'amount' => 'integer',
        'response' => 'array',
    ];
}

public function user(): BelongsTo
{
    return $this->belongsTo(User::class);
}

public function bankAccount(): BelongsTo
{
    return $this->belongsTo(BankAccount::class);
}
```

- `casts()` method (not `protected $casts` array)
- Explicit return types on every relationship: `BelongsTo`, `HasMany`, `HasOne`, `BelongsToMany`, `MorphMany`, etc. - import from `Illuminate\Database\Eloquent\Relations\*`
- Accessors use `get{Name}Attribute(): {type}` with explicit return type

## Older style (still present, e.g. `app/Models/Account.php`, `app/Models/Transaction.php`)

```php
protected $casts = [...];

public function group()
{
    return $this->belongsTo(AccountGroup::class, 'group_id');
}
```

If you're adding ONE new relationship/method to an old-style file, match the file's existing style for consistency rather than mixing both - but mention to the user that the newer convention exists, in case they want the whole file modernized separately.

## General Rules

- Always use `HasFactory` + create/update the matching factory when adding a new model (per CLAUDE.md "Model Creation").
- PHPDoc comments on relationships explaining the Vietnamese business meaning are common (e.g. `/** Thuộc về một người dùng */`) - keep this pattern for non-obvious relationships.
- Eager-load relationships used in views/loops (`->with(...)`) to avoid N+1 - check the controller for `->with()` chains before adding a new relationship access in a loop.
- Custom query logic belongs in scopes (`scopeXxx(Builder $query): Builder`) on the model, not raw `DB::` queries (per CLAUDE.md "Avoid `DB::`; prefer `Model::query()`").

## If Unclear

If a model mixes conventions and it's not clear whether to modernize it as part of your change, ask the user - don't do a drive-by rewrite of unrelated methods.

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 databases →

Comments

Loading comments…

Newsletter

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