How to Use Service and Clean Controller in Laravel
Using the laravel service we will clean the controller code very easily. This post explain how to create and use laravel service class for clean the controller code. The main reason to use laravel service is clean code, reuseable code, write less code, more readable code etc. We will create one or more service class for each controller and clean the controller code with the service classes. If we use the service we must call the service class from the controller.
At first we will create a service class manually:
Firstly we will create Services directory inside app\Htpp directory also create a php class StoreProductService.php inside app\http\Services. In this service class we will create methods as much as we need.
Keep in mind all service class must be create under app\Http directory.
To understand the service magic we need some codes in a controller. Let's see the below controller script.
StoreProductController.php
namespace App\Http\Controllers;
use Illuminate\Support\Str;
use App\Models\Product;
class ProductController extends Controller
{
public function store_new_product(Request $request)
{
$imageName = '';
if ($image = $request->file('image')) {
$imageName = time() . '-' . uniqid() . '.' . $image->getClientOriginalExtension();
$image->move('assets/uploads/product', $imageName);
}
Product::create([
'title' => $request->title,
'slug' => Str::slug($request->title),
'description' => $request->description,
'image' => $imageName,
'category' => $request->category,
]);
toastr_success('New Product Successfully Created');
return back();
}
}
Now create one or more functions according to your needs inside StoreProductService and update the method with the given code. Here we just move the controller code into the below service class.
StoreProductService.php
<?php
namespace App\Http\Services;
class StoreProductService
{
public function store_new_product($request)
{
$imageName = '';
if ($image = $request->file('image')) {
$imageName = time() . '-' . uniqid() . '.' . $image->getClientOriginalExtension();
$image->move('assets/uploads/product', $imageName);
}
Product::create([
'title' => $request->title,
'slug' => Str::slug($request->title),
'description' => $request->description,
'image' => $imageName,
'category' => $request->category,
]);
toastr_success('New Product Successfully Created');
return back();
}
}
Now we will clean our controller, to do this call the service class from StoreProductController.php that we created before and chaining the method from service class. Thus we can clean the controller with the help of service.
namespace App\Http\Controllers;
use Illuminate\Support\Str;
use App\Models\Product;
class ProductController extends Controller
{
public function store_new_product(Request $request)
{
return (new StoreProductService)->store_new_pproduct($request);
}
}
Looks now controller is very clean and more readable. This way using the service we can clean and readable our controller.
If you like what you are reading, please think about buying us a coffee as a token of appreciation.
We appreciate your support and are committed to providing you useful and informative content.
We are thankful for your ongoing support
You May Like These:
Laravel 10 multiple form validation on the same page-WebJourney
Laravel 9 ajax form validation and display error messages
How to Create and Use Custom Request for smart validation in Laravel