Skip to content
Laravel PHP

Laravel CRUD Best Practices: Resource Controllers and FormRequests

Raheel Shan

Raheel Shan

April 6th, 2025 · 10 min read

Laravel CRUD Best Practices: Resource Controllers and FormRequests

Laravel CRUD is easiest to maintain when every controller method has the same shape: a resource controller routes the request, a FormRequest class validates it and does the work, and the controller only redirects or returns a view. This post shows that pattern with working code for current Laravel, so every model in your app is built the same way.

Quick answer

  1. Generate a resource controller with php artisan make:controller PostController --resource and register it with Route::resource.
  2. Create one FormRequest class per action (list, get, create, update, delete).
  3. Put validation in rules() and the one-line query in a handle() method.
  4. In the controller, type-hint the request, call handle(), then return a view or redirect.

Laravel CRUD controller: generate the resource controller

The command php artisan make:controller PostController --resource creates a controller with seven methods: index, create, store, show, edit, update and destroy. Add --model=Post if you want route model binding in the stubs.

php artisan make:controller PostController --resource

Register all seven routes with one line:

use App\Http\Controllers\PostController;

Route::resource('posts', PostController::class);

Route names follow the resource name: posts.index, posts.create, posts.store, posts.show, posts.edit, posts.update and posts.destroy. Run php artisan route:list to see them.

Create the FormRequest classes

Generate one FormRequest per action:

php artisan make:request Post/GetAllPostsRequest
php artisan make:request Post/GetPostRequest
php artisan make:request Post/CreatePostRequest
php artisan make:request Post/UpdatePostRequest
php artisan make:request Post/DeletePostRequest

I use these classes as small command objects. Laravel still runs authorize() and rules() before the controller body, and I add a handle() method that does the actual work. That gives business logic one obvious home, which I explain in this post on business logic.

The lean controller

Every method looks the same: call handle(), then return something.

use App\Http\Requests\Post\CreatePostRequest;
use App\Http\Requests\Post\DeletePostRequest;
use App\Http\Requests\Post\GetAllPostsRequest;
use App\Http\Requests\Post\GetPostRequest;
use App\Http\Requests\Post\UpdatePostRequest;

class PostController extends Controller
{
    public function index(GetAllPostsRequest $request)
    {
        return view('posts.index', ['posts' => $request->handle()]);
    }

    public function create(GetPostRequest $request)
    {
        return view('posts.create', ['post' => $request->handle()]);
    }

    public function store(CreatePostRequest $request)
    {
        $request->handle();

        return redirect()->route('posts.index')
            ->with('message', 'Post has been saved successfully.');
    }

    public function show(int $id, GetPostRequest $request)
    {
        return view('posts.show', ['post' => $request->handle($id)]);
    }

    public function edit(int $id, GetPostRequest $request)
    {
        return view('posts.edit', ['post' => $request->handle($id)]);
    }

    public function update(int $id, UpdatePostRequest $request)
    {
        $request->handle($id);

        return redirect()->route('posts.index')
            ->with('message', 'Post has been updated successfully.');
    }

    public function destroy(int $id, DeletePostRequest $request)
    {
        $request->handle($id);

        return redirect()->route('posts.index')
            ->with('message', 'Post has been deleted successfully.');
    }
}

Flash messages and redirects are set in one place, and the controller holds no queries and no validation.

Read records: list and get

The list request handles filtering and pagination:

use App\Models\Post;
use Illuminate\Foundation\Http\FormRequest;

class GetAllPostsRequest extends FormRequest
{
    public function rules(): array
    {
        return [
            'status' => ['sometimes', 'integer'],
        ];
    }

    public function handle()
    {
        $posts = Post::latest('id');

        if ($this->filled('status')) {
            $posts->where('status', $this->integer('status'));
        }

        return $posts->paginate(15);
    }
}

Move the filter into a local scope when you have more than one or two.

The get request serves create, show and edit:

class GetPostRequest extends FormRequest
{
    public function handle(?int $id = null)
    {
        return $id ? Post::findOrFail($id) : new Post;
    }
}

When $id is empty you get a blank model for the create form. When it is set you get the record, or a 404 if it does not exist. In the Blade form, old() keeps the visitor's input after a failed validation and falls back to the database value in edit mode:

<input type="text" name="title" value="{{ old('title', $post->title) }}">

You can shorten the get request to Post::findOrNew($id), but note that it returns an empty model for an id that does not exist, so a bad edit URL shows an empty form instead of a 404. I cover that trade-off in part 2 of this series on findOrNew and old().

Write records: validate, then save

Validation rules are arrays of rule names. In rules() return one entry per field:

class CreatePostRequest extends FormRequest
{
    public function authorize(): bool
    {
        return true;
    }

    public function rules(): array
    {
        return [
            'title' => ['required', 'string', 'max:255'],
            'content' => ['required', 'string'],
        ];
    }

    public function handle(): Post
    {
        return Post::create($this->validated());
    }
}

Laravel validates before handle() runs. If validation fails, the visitor is redirected back with errors and the old input, and handle() is never called. validated() returns only the fields that passed, so a stray is_admin field in the request never reaches the model. Add the fields to $fillable on the model so create() accepts them.

The update request uses the same rules:

class UpdatePostRequest extends FormRequest
{
    public function rules(): array
    {
        return [
            'title' => ['required', 'string', 'max:255'],
            'content' => ['required', 'string'],
        ];
    }

    public function handle(int $id): Post
    {
        $post = Post::findOrFail($id);
        $post->update($this->validated());

        return $post;
    }
}

Loading the model first (instead of Post::where('id', $id)->update(...)) means model events, observers and casts run, and a missing id returns a 404. The delete request is just as short:

class DeletePostRequest extends FormRequest
{
    public function handle(int $id): void
    {
        Post::findOrFail($id)->delete();
    }
}

Why use FormRequests for CRUD

  • Authorization: each class has an authorize() method, for example return $this->user()->can('view-post'); or a Bouncer check.
  • One place for logic: validation and the query live together, away from the controller.
  • Identical structure: every model gets the same five classes, so the pattern is easy to remember and to generate.
  • Web and API from one method: a small helper can return JSON for api/* requests and a view otherwise.
class ResponseHelper
{
    public static function handle(string $view, $data)
    {
        if (request()->is('api/*')) {
            return response()->json($data);
        }

        return view($view, $data);
    }
}

When not to use this pattern

Five request classes per model is more files than a two-line controller needs. For a tiny admin screen or a prototype, validating inline with $request->validate([...]) in the controller is fine. Use the FormRequest approach when the app will grow, when several people work in it, or when you want every resource to follow one rule. If you want to go further and share the queries across the whole app, see the AQC CRUD implementation guide.

FAQ

How do I create CRUD in Laravel? Generate a resource controller with php artisan make:controller PostController --resource, register it with Route::resource('posts', PostController::class), then fill in the seven methods and create the views.

What is the command for a CRUD controller in Laravel? php artisan make:controller PostController --resource. Add --model=Post for route model binding, or --api to skip the create and edit methods.

What are Laravel CRUD best practices? Keep controllers thin, validate with FormRequest classes, use validated() instead of all(), set $fillable on models, return 404 for missing records with findOrFail(), and use the same structure for every resource.

Should validation go in the controller or a FormRequest? Use a FormRequest when the same rules are reused or the controller is growing. Inline validation is fine for one-off forms.

Does a FormRequest run before the controller method? Yes. Laravel resolves the type-hinted FormRequest, runs authorize() and rules(), and only then calls your controller method.

Structure your Laravel app with me

Fat controllers, business logic in five places, views that accept any array. Bring your codebase or your plan.

In 60 minutes I review how it is laid out and we settle modules, DTOs, ViewModels and clean controllers.

Book a Laravel Architecture call

Found this useful?

Support the work that goes into these posts.

Support
Comments


Comment created and will be displayed once approved.