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
Comments (0)
No comments yet. Be the first to comment!