agyanadda@gmail.com

Laravel CRUD Operations

Database and Eloquent 1 min read Updated Dec 2, 2026 Topic 9 of 13

CRUD means Create, Read, Update and Delete — the four things almost every admin panel does. In Laravel, a resource controller plus an Eloquent model gives you all four.

Steps

  1. php artisan make:model Post -m and write the migration
  2. php artisan make:controller PostController --resource --model=Post
  3. Route::resource('posts', PostController::class);
  4. Create Blade views: index, create, edit, show

Route model binding

When a method has a Post $post parameter, Laravel loads the post from the {post} id in the URL automatically — or shows 404.

Forms for PUT and DELETE

HTML forms only send GET and POST. Add @method('PUT') or @method('DELETE') inside a POST form, and Laravel treats it as that method. Every form also needs @csrf.

Flash messages

After saving, redirect with a message: return redirect()->route('posts.index')->with('success', 'Post saved'); and show session('success') in the view.

Example

Example — store, update and destroy PHP
<?php
public function store(Request $request)
{
    $data = $request->validate([
        'title' => 'required|max:255',
        'body'  => 'required',
    ]);
    $request->user()->posts()->create($data);

    return redirect()->route('posts.index')->with('success', 'Post created');
}

public function update(Request $request, Post $post)
{
    $post->update($request->validate(['title' => 'required|max:255', 'body' => 'required']));

    return redirect()->route('posts.show', $post)->with('success', 'Post updated');
}

public function destroy(Post $post)
{
    $post->delete();

    return back()->with('success', 'Post deleted');
}

/* Delete button in Blade:
<form method="POST" action="{{ route('posts.destroy', $post) }}">
    @csrf
    @method('DELETE')
    <button>Delete</button>
</form> */

Post $post is filled by route model binding. The delete form is a POST form that Laravel treats as DELETE because of @method('DELETE').

Test Yourself With Exercises

Exercise 1 Multiple Choice

How does a Blade form send a DELETE request?

Exercise 2 True / False

Route model binding returns a 404 page if the id does not exist.

Exercise 3 Fill in the Blank

Every POST form in Laravel needs the @___ directive.

Frequently Asked Questions

The form is missing @csrf, or the session expired. Add @csrf inside the form and reload the page before submitting again.