CRUD with Eloquent
Create, read, update, and delete records using Eloquent methods.
CRUD with Eloquent is a free PHP Academy lesson on CoddyKit — lesson 2 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the PHP Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
Create — save()
Create a model instance, set attributes, and call save().
<?php
$post = new Post();
$post->title = "Hello World";
$post->body = "My first post";
$post->save(); // INSERT INTO posts ...Create — create()
Model::create() inserts a row in one line using mass assignment. Requires $fillable to be set.
<?php
$post = Post::create([
"title" => "Hello World",
"body" => "My first post",
"user_id" => auth()->id(),
]);firstOrCreate and updateOrCreate
firstOrCreate() finds a matching record or creates it. updateOrCreate() finds and updates or creates.
<?php
// Find or create:
$tag = Tag::firstOrCreate(["name" => "PHP"]);
// Update or create:
$setting = Setting::updateOrCreate(
["key" => "theme"],
["value" => "dark"]
);Read — all()
Retrieve all rows as an Eloquent Collection.
<?php
$posts = Post::all(); // SELECT * FROM posts
foreach ($posts as $post) {
echo $post->title;
}Read — find()
Find a record by primary key. Returns null if not found. findOrFail() throws a 404 exception if not found.
<?php
$post = Post::find(1);
$post = Post::findOrFail(1); // throws ModelNotFoundException if missingRead — where()
Filter records with where() clauses. Chain multiple conditions.
<?php
$published = Post::where("is_published", true)
->where("user_id", $userId)
->orderBy("created_at", "desc")
->get();Update — save()
Modify attributes on an existing model and call save() — generates an UPDATE.
<?php
$post = Post::findOrFail(1);
$post->title = "Updated Title";
$post->save(); // UPDATE posts SET title=... WHERE id=1Update — update()
Mass-update multiple records matching a query in one SQL call.
<?php
Post::where("user_id", $userId)
->update(["is_published" => false]);Delete — delete()
Delete a retrieved model instance.
<?php
$post = Post::findOrFail(1);
$post->delete(); // DELETE FROM posts WHERE id=1Delete — destroy()
Post::destroy($id) deletes by primary key without fetching the model first.
<?php
Post::destroy(1);
Post::destroy([1, 2, 3]); // delete multiplePagination
Use paginate() to return a paginator object with automatic LIMIT/OFFSET.
<?php
$posts = Post::latest()->paginate(15);
// In Blade: {{ $posts->links() }}Summary
Eloquent provides create() / save() for inserts, find() / where() for reads, save() / update() for updates, and delete() / destroy() for deletes.
Quick Check
Which method throws an exception if the record is not found?
Frequently asked questions
Is the “CRUD with Eloquent” lesson free?
Yes — the full text of “CRUD with Eloquent” is free to read here on the web, and the PHP Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the PHP Academy course, upgrade to CoddyKit PRO.
What will I learn in “CRUD with Eloquent”?
Create, read, update, and delete records using Eloquent methods. You practise PHP Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start PHP Academy?
No prior experience is required. PHP Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 2 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “CRUD with Eloquent” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this PHP Academy lesson?
Yes. Every PHP Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.