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
php artisan make:model Post -mand write the migrationphp artisan make:controller PostController --resource --model=PostRoute::resource('posts', PostController::class);- 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
<?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').