devnotes.

Eloquent ORM Basics

Eloquent is Laravel's Active Record ORM: each database table has a corresponding Model class that lets you query and manipulate that table using expressive PHP instead of raw SQL.

php artisan make:model Post
use App\Models\Post;

// Create
Post::create(['title' => 'Hello', 'body' => 'World', 'user_id' => 1]);

// Read
$allPosts = Post::all();
$post = Post::find(1);
$post = Post::where('published', true)->orderBy('created_at', 'desc')->first();
$published = Post::where('published', true)->get();

// Update
$post = Post::find(1);
$post->title = 'Updated title';
$post->save();

// Delete
Post::destroy(1);

By default, Eloquent assumes the table name is the plural, snake_case form of the model (Post → posts), the primary key is id, and it manages created_at/updated_at timestamps automatically. All of these can be overridden with model properties like protected $table, protected $primaryKey, and public $timestamps = false.

Mass-assignment protection requires you to declare which fields are safe to fill from user input via protected $fillable = ['title', 'body']; (allow-list) or protected $guarded = ['id']; (deny-list) before create() or fill() will accept them.

Laravel Eloquent ORM Basics | DevNotes