$120 tested Claude codes · real before/after data · Full tier $15 one-timebuy --sheet=15 →
$Free 40-page Claude guide — setup, 120 prompt codes, MCP servers, AI agents. download --free →
clskills.sh — terminal v2.4 — 2,347 skills indexed● online
[CL]Skills_
LaravelintermediateNew

Laravel Eloquent

Share

Write Eloquent models with relationships and scopes

Works with OpenClaude

You are a Laravel backend developer. The user wants to write Eloquent models with relationships and scopes that handle complex data retrieval and filtering patterns.

What to check first

  • Run php artisan tinker to verify Eloquent is available and models load correctly
  • Check your database migrations exist by running php artisan migrate:status
  • Verify the database connection in .env file matches your setup

Steps

  1. Create a new model with php artisan make:model Post -m to generate both model and migration file
  2. Define table name explicitly in the model if it doesn't follow Laravel's plural convention using protected $table = 'custom_posts'
  3. Create relationships in the model using methods like hasMany(), belongsTo(), or belongsToMany() with return type declarations
  4. Define query scopes as public methods prefixed with scope (e.g., scopeActive()) that accept Builder $query as first parameter
  5. Use scope chaining in queries with ->active()->recent()->get() to combine multiple scopes
  6. Add fillable or guarded properties to enable mass assignment: protected $fillable = ['title', 'content', 'user_id']
  7. Cast attributes using $casts array for type safety: 'published_at' => 'datetime'
  8. Test relationships with php artisan tinker using Post::with('author', 'comments')->first()

Code

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\Builder;

class Post extends Model
{
    protected $fillable = ['title', 'content', 'user_id', 'published_at'];

    protected $casts = [
        'published_at' => 'datetime',
        'is_featured' => 'boolean',
    ];

    public function author(): BelongsTo
    {
        return $this->belongsTo(User::class, 'user_id');
    }

    public function comments(): HasMany
    {
        return $this->hasMany(Comment::class);
    }

    public function scopeActive(Builder $query): Builder
    {
        return $query->whereNotNull('published_at')
                     ->where('published_at', '<=', now());
    }

    public function scopeRecent(Builder $query): Builder
    {
        return $query->orderBy('published_at', 'desc');
    }

    public function scopeByAuthor(Builder $query, User $user): Builder
    {
        return $query->where('user_id', $user->id);
    }

    public function scopeFeatured(Builder $query): Builder
    {
        return $query->where('is_featured

Note: this example was truncated in the source. See the GitHub repo for the latest full version.

Common Pitfalls

  • Treating this skill as a one-shot solution — most workflows need iteration and verification
  • Skipping the verification steps — you don't know it worked until you measure
  • Applying this skill without understanding the underlying problem — read the related docs first

When NOT to Use This Skill

  • When a simpler manual approach would take less than 10 minutes
  • On critical production systems without testing in staging first
  • When you don't have permission or authorization to make these changes

How to Verify It Worked

  • Run the verification steps documented above
  • Compare the output against your expected baseline
  • Check logs for any warnings or errors — silent failures are the worst kind

Production Considerations

  • Test in staging before deploying to production
  • Have a rollback plan — every change should be reversible
  • Monitor the affected systems for at least 24 hours after the change

Quick Info

CategoryLaravel
Difficultyintermediate
Version1.0.0
AuthorClaude Skills Hub
laraveleloquentorm

Install command:

curl -o ~/.claude/skills/laravel-eloquent.md https://clskills.in/skills/laravel/laravel-eloquent.md

Related Laravel Skills

Other Claude Code skills in the same category — free to download.

Want a Laravel skill personalized to YOUR project?

This is a generic skill that works for everyone. Our AI can generate one tailored to your exact tech stack, naming conventions, folder structure, and coding patterns — with 3x more detail.