How To Use a Custom CRUD Service and Laravel Request to Do CRUD

Welcome to the gruntblog

  

Laravel is one of the most popular PHP frameworks out there today. It’s trusted by developers all over the world for building everything from small personal projects to massive enterprise systems. With its clean syntax, powerful features, and a massive community behind it, Laravel makes web development not only faster β€” but also way more enjoyable.

Whether you’re building a simple blog, an API, or a full-blown CMS, Laravel gives you everything you need straight out of the box β€” routing, authentication, caching, queues, mail, and more. Plus, its expressive syntax lets you focus on what really matters: writing clean, meaningful code instead of wrestling with boilerplate.

In short, Laravel helps you build modern web apps with confidence and efficiency πŸš€

In this tutorial, we’ll take a look at how to simplify your controllers using a Custom CRUD Service combined with Laravel Form Requests. This approach helps you:

  • βœ… Keep your controllers slim and clean βœ… Centralize your database logic βœ… Reuse validation rules easily βœ… Make your codebase easier to maintain

START TUTORIAL

Before we begin lets start createing our project first

                                composer create-project laravel/laravel crud-tutorial
                            

once the project is made 

                                 πŸ“‚ app
└── πŸ“‚ Http/
β”‚  └── πŸ“‚ Controllers/
β”‚    β”œβ”€β”€ πŸ“„ Controller.php
β”‚    β”œβ”€β”€ πŸ“„ PostController.php
β”‚  └── πŸ“‚ Requests/
β”‚    β”œβ”€β”€ πŸ“„ PostRequest.php πŸ‘ˆ our PostRequest 
└── πŸ“‚ Models/
β”‚  β”œβ”€β”€ πŸ“„ Post.php
β”‚  β”œβ”€β”€ πŸ“„ User.php
└── πŸ“‚ Providers/
β”‚  β”œβ”€β”€ πŸ“„ AppServiceProvider.php
└── πŸ“‚ services/
β”‚  └── πŸ“„ CrudService.php πŸ‘ˆ crud service is located
                            

Create Folder Called Services follow exact same folder structure just like this

Now lets use laravel command to create request 

                                php artisan make:request PostRequest

                            

copy this code inside the request

                                <?php

namespace App\Http\Requests;

use Illuminate\Foundation\Http\FormRequest;

class PostRequest extends FormRequest
{
    public function authorize(): bool
    {
        return true; // handle auth later
    }

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

        ];
    }


}

                            

and lets make our migrations and models and copy everything to the corresponding files

                                php artisan make:mode Post -mc 
                            

mode/Post.php

                                <?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class Post extends Model
{
    use HasFactory;

    // βœ… Allow mass assignment for these columns
    protected $fillable = [
        'title',
        'content',
    ];
}

                            

app/http/controller/PostController.php

                                <?php

namespace App\Http\Controllers;

use App\Models\Post;
use App\Http\Requests\PostRequest;
use App\Services\CrudService;

class PostController extends Controller
{
    protected $crud;

    public function __construct(CrudService $crud)
    {
        $this->crud = $crud;
    }

    public function index()
    {
        $posts = $this->crud->getAll();
        return view('welcome', compact('posts'));
    }

    public function store(PostRequest $request)
    {
        $this->crud->create($request->validated());
        return redirect()->route('posts.index')->with('success', 'Post created successfully!');
    }

    public function edit(Post $post)
    {
        $posts = $this->crud->getAll();
        return view('welcome', ['postToEdit' => $post, 'posts' => $posts]);
    }

    public function update(PostRequest $request, Post $post)
    {
        $this->crud->update($post, $request->validated());
        return redirect()->route('posts.index')->with('success', 'Post updated successfully!');
    }

    public function destroy(Post $post)
    {
        $this->crud->delete($post);
        return redirect()->route('posts.index')->with('success', 'Post deleted successfully!');
    }
}

                            

πŸš€ In Short

  • Form Requests handle validation.
  • Services handle business logic.
  • Controllers just connect the dots.

Together they form a clean, testable, and scalable pattern for any Laravel app β€” whether you’re managing blog posts, users, or full-blown modules.

heres the working files for the project

i hope this tutorial will at least help . in our next one we gonna expand this tutorial . happy coding bro xD