Skip to content
Guest
Laravel · Course Syllabus Show lessons
Database & Eloquent · 8 min read · Intermediate

Eloquent ORM CRUD, Mass Assignment & Scopes

Query, create, update, and filter records cleanly with Eloquent while preventing mass-assignment bugs.

What you'll learn

  • Protect models against mass-assignment with `$fillable`
  • Encapsulate repeated query filters using local scopes (`scopePublished`)
  • Use `paginate()` instead of `all()` on user-facing lists

Overview

Query, create, update, and filter records cleanly with Eloquent while preventing mass-assignment bugs.

Code Example

<?php
namespace App\Models;

use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;

class Course extends Model {
    protected $fillable = ['title', 'slug', 'is_published'];

    public function scopePublished(Builder $query): void {
        $query->where('is_published', true);
    }
}

// Clean reusable query
$courses = Course::published()->latest()->paginate(12);

Key Takeaways

  • Protect models against mass-assignment with $fillable
  • Encapsulate repeated query filters using local scopes (scopePublished)
  • Use paginate() instead of all() on user-facing lists

Quick Check

Check Your Understanding

Score: 0 / 1

1. Why is `User::create($request->all())` dangerous if `$fillable` is not properly restricted?

Study Workspace

Finished this lesson?

Your progress is saved locally in your browser, no account needed.

Search courses, lessons, and references

Type to search across all courses, lessons, and quick references.

Press Esc to close Powered by Pagefind