Backend Models

Design Eloquent database models with relationships, casts, and validation.

Updated Oct 31, 2025
One-click install
npx skills add https://github.com/FlorianRiquelme/statamic-assets --skill backend-models-florianriquelme
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: Backend Models
Source: https://github.com/FlorianRiquelme/statamic-assets/tree/main/.claude/skills/backend-models
Command: npx skills add https://github.com/FlorianRiquelme/statamic-assets --skill backend-models-florianriquelme

SYSTEM DOCUMENTATION & REQUIREMENTS

What problem does it solve?

This Skill guides the creation of well-structured and maintainable database models, ensuring data integrity, proper relationships, and consistent data handling within your application.

Core Features & Use Cases

  • Data Integrity: Defines proper data types, database constraints, and multi-layer validation for reliable data.
  • Relationship Management: Clearly defines Eloquent relationships (e.g., hasMany, belongsTo) for intuitive data access.
  • Use Case: When creating a new Product model, this skill ensures it has appropriate fields, defines relationships to Category and Order models, and includes mass assignment protection for secure data handling.

Quick Start

Create a new Eloquent model for a 'Book' with 'title', 'author_id', and 'published_date' fields, defining a 'belongsTo' relationship to an 'Author' model.

Frequently Asked Questions about Backend Models

High-intent search queries and answers about installing and using this skill.

FAQPage Schema
How do I define relationships between Laravel Eloquent models?

Eloquent relationships like hasMany, belongsTo, and belongsToMany let you connect models intuitively. Define them as methods in your model class to establish data connections—for example, a User hasMany Posts, and each Post belongsTo a User, enabling chainable queries across related data.

What's the best way to ensure data integrity in database models?

Enforce data integrity through multiple layers: define correct data types and database constraints in migrations, use mass-assignment protection with fillable or guarded properties, apply attribute casting for type safety, and add validation logic to reject invalid input before persistence.

How do I set up mass assignment protection in Laravel models?

Use the fillable property to whitelist attributes users can mass-assign, or use guarded to blacklist specific attributes. This prevents unauthorized fields from being set through assignment, protecting sensitive columns like admin flags or encrypted data from unintended modification.

Can I use accessors and mutators to transform model attributes?

Yes. Accessors retrieve and transform attribute values when accessed, while mutators modify values before storage. Use them to format dates, hash passwords, encrypt sensitive data, or compute derived properties without altering the underlying database column.

Do I need database constraints if I define Eloquent relationships?

Yes. Eloquent relationships define application-level connections, but database constraints (foreign keys, unique, not null) enforce data integrity at the database layer, preventing orphaned records and invalid states that application logic alone cannot protect.

How do I create model factories and seeders for testing?

Factories generate fake model instances with realistic test data; seeders populate your database in bulk. Define factory attributes in database/factories, then use seeders to call factories and insert records, enabling consistent, repeatable test environments without manual record creation.