Eloquent CRUD: Create, Read, Update, and Delete
Lesson 6 of 33 in the Laravel learning path.
How do I create, find, update, and delete a Lesson with Eloquent?
Eloquent maps the lessons table to the Lesson model. Create inserts a row from an array, findOrFail loads one row or returns 404, update writes changed columns, and delete removes the row. Those four calls replace hand-written INSERT and SELECT statements for the catalog's ordinary screens.
Mass assignment is the sharp edge. create and update ignore keys that are not in $fillable, but only if you do not pass a raw request array that includes columns you later add to $fillable by habit. Pass validated data, list the columns you intend to write, and set user_id yourself.
Syntax & Parameters
Lesson::query()->create($data); $lesson->update($data); $lesson->delete();
Example
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class Lesson extends Model
{
protected $fillable = ['title', 'track', 'seats', 'slug', 'user_id'];
}
namespace App\Http\Controllers;
use App\Http\Requests\StoreLessonRequest;
use App\Http\Requests\UpdateLessonRequest;
use App\Models\Lesson;
use Illuminate\Http\RedirectResponse;
use Illuminate\View\View;
class LessonController extends Controller
{
public function index(): View
{
$lessons = Lesson::query()->latest('id')->paginate(15);
return view('lessons.index', ['lessons' => $lessons]);
}
public function show(Lesson $lesson): View
{
return view('lessons.show', ['lesson' => $lesson]);
}
public function store(StoreLessonRequest $request): RedirectResponse
{
$lesson = Lesson::query()->create([
...$request->validated(),
'slug' => str($request->validated('title'))->slug()->toString(),
'user_id' => $request->user()->id,
]);
return redirect()->route('lessons.show', $lesson);
}
public function update(UpdateLessonRequest $request, Lesson $lesson): RedirectResponse
{
$this->authorize('update', $lesson);
$lesson->update($request->validated());
return redirect()->route('lessons.show', $lesson);
}
public function destroy(Lesson $lesson): RedirectResponse
{
$this->authorize('delete', $lesson);
$lesson->delete();
return redirect()->route('lessons.index');
}
}
Expected Output
POST title=Indexes track=sql seats=20 INSERT lessons and redirect to the new slug PUT title=Indexes and plans UPDATE that row's title 302 back to the show page DELETE row gone 302 /lessons GET /lessons/99999 when using findOrFail 404
Line-by-Line Walkthrough
- $fillable is the allow-list for create and update. Columns not listed are dropped.
- index uses the query builder and paginate so the page does not load the whole table.
- show receives the bound model. findOrFail is what the binder does for you.
- store spreads validated input, then sets slug and user_id so those values win over anything in the form.
- The spread copies the three validated keys into the insert array, and the later keys overwrite slug and user_id.
- update authorizes first. A student who posts to the endpoint gets 403 and the row stays as it was.
- update writes only validated keys, which on the edit form should be title, track, and seats.
- destroy authorizes with the delete ability, which can be stricter than update.
- delete removes that model. It is not Lesson::query()->delete(), which would empty the table.
- The index redirect after delete avoids a 404 from sending the browser back to the row you just removed.
- Timestamps are maintained by Eloquent when the table has created_at and updated_at.
- A missing lesson id on a manually built findOrFail($id) throws and becomes a 404, the same as binding.
Common Pitfalls & Security
Lesson::query()->delete() with no where clause deletes every lesson. Always call delete on the model instance you authorized.
- $guarded = [] turns mass assignment off and makes the next $request->all() able to set is_admin or user_id.
- Adding user_id to $fillable and then spreading $request->all() lets the client transfer ownership.
- update without authorize changes another instructor's course.
- Deleting in the browser with a GET link is crawled and repeated. Use a POST form with @method('DELETE') and @csrf.
- paginate() in the controller and ::all() in the view loads two different sets. Pass the paginator through.
- Calling save() after setting attributes one by one is fine, but do not set $lesson->exists = false unless you intend to insert a second row.
Best Practices & Modern Standards
On Laravel 12, keep $fillable explicit. Prefer Lesson::query()->create() and $lesson->update() with validated arrays. Use route model binding for the id, and findOrFail only when the id comes from somewhere that is not a route parameter.
Soft deletes are a later choice for a catalog that must undo a removal. A classroom catalog can start with real deletes once destroy is authorized and confirmed. Add an index on user_id if instructor dashboards filter by owner.
Real-World Use
An instructor adds a SQL lesson, fixes the title after class, and removes a draft they created by mistake. Each action is one controller method. Students hitting the same URLs fail the policy. The catalog index stays paginated when the department has more than a page of lessons.
Frequently Asked Questions
What is the difference between create and save?
create inserts immediately from an array and respects $fillable. new Lesson followed by save inserts after you set attributes yourself. Both write a row. create is the shorter path for validated input.
Does update return the model?
$lesson->update() returns true or false. The same $lesson variable is already the model, and its attributes are refreshed from the array you passed. Redirect with $lesson, not with the boolean.
How do I stop a missing id from becoming a blank page?
Use route model binding or findOrFail. Both throw ModelNotFoundException, which Laravel renders as 404. find returns null, and calling methods on that null is a 500.
Should I mass-assign user_id?
You can list it in $fillable and still set it only in your own array, as store does above. Never copy it from the request. The signed-in user is the source.