Lädt...

🔧 How to Use Repository and Service Pattern in Laravel


Nachrichtenbereich: 🔧 Programmierung
🔗 Quelle: dev.to

The Repository and Service Pattern is a powerful way to structure your Laravel applications. It promotes clean, maintainable, and testable code by separating concerns and decoupling your application logic. In this guide, we’ll walk through how to implement this pattern in Laravel with a simple CRUD example for managing products.

Step 1: Create Repository Interface

The first step is to define an interface for the repository. This ensures that the repository adheres to a contract, making it easier to swap implementations later.

Create the file app/Repositories/Interfaces/ProductRepositoryInterface.php:

namespace App\Repositories\Interfaces;

interface ProductRepositoryInterface
{
    public function getAll();
    public function findById($id);
    public function create(array $data);
    public function update($id, array $data);
    public function delete($id);
}

Step 2: Create Repository Implementation

Next, implement the repository interface. This class will handle all database interactions for the Product model.

Create the file app/Repositories/ProductRepository.php:

namespace App\Repositories;

use App\Models\Product;
use App\Repositories\Interfaces\ProductRepositoryInterface;

class ProductRepository implements ProductRepositoryInterface
{
    public function getAll()
    {
        return Product::all();
    }

    public function findById($id)
    {
        return Product::findOrFail($id);
    }

    public function create(array $data)
    {
        return Product::create($data);
    }

    public function update($id, array $data)
    {
        $product = Product::findOrFail($id);
        $product->update($data);
        return $product;
    }

    public function delete($id)
    {
        return Product::destroy($id);
    }
}

Step 3: Create Service Interface

The service layer acts as an intermediary between the controller and the repository. It contains the business logic of your application.

Create the file app/Services/Interfaces/ProductServiceInterface.php:

namespace App\Services\Interfaces;

interface ProductServiceInterface
{
    public function getAllProducts();
    public function getProductById($id);
    public function createProduct(array $data);
    public function updateProduct($id, array $data);
    public function deleteProduct($id);
}

Step 4: Create Service Implementation

Now, implement the service interface. This class will use the repository to perform operations.

Create the file app/Services/ProductService.php:

namespace App\Services;

use App\Repositories\Interfaces\ProductRepositoryInterface;
use App\Services\Interfaces\ProductServiceInterface;

class ProductService implements ProductServiceInterface
{
    protected $productRepository;

    public function __construct(ProductRepositoryInterface $productRepository)
    {
        $this->productRepository = $productRepository;
    }

    public function getAllProducts()
    {
        return $this->productRepository->getAll();
    }

    public function getProductById($id)
    {
        return $this->productRepository->findById($id);
    }

    public function createProduct(array $data)
    {
        return $this->productRepository->create($data);
    }

    public function updateProduct($id, array $data)
    {
        return $this->productRepository->update($id, $data);
    }

    public function deleteProduct($id)
    {
        return $this->productRepository->delete($id);
    }
}

Step 5: Bind Interfaces to Implementations

To make the repository and service injectable, bind the interfaces to their implementations in the AppServiceProvider.

Modify app/Providers/AppServiceProvider.php:

namespace App\Providers;

use Illuminate\Support\ServiceProvider;
use App\Repositories\Interfaces\ProductRepositoryInterface;
use App\Repositories\ProductRepository;
use App\Services\Interfaces\ProductServiceInterface;
use App\Services\ProductService;

class AppServiceProvider extends ServiceProvider
{
    public function register()
    {
        $this->app->bind(ProductRepositoryInterface::class, ProductRepository::class);
        $this->app->bind(ProductServiceInterface::class, ProductService::class);
    }

    public function boot()
    {
        //
    }
}

Step 6: Update the Controller

Now, update the ProductController to use the service layer. This keeps the controller lightweight and focused on handling HTTP requests.

Modify app/Http/Controllers/ProductController.php:

namespace App\Http\Controllers;

use App\Services\Interfaces\ProductServiceInterface;
use Illuminate\Http\Request;

class ProductController extends Controller
{
    protected $productService;

    public function __construct(ProductServiceInterface $productService)
    {
        $this->productService = $productService;
    }

    public function index()
    {
        return response()->json($this->productService->getAllProducts());
    }

    public function show($id)
    {
        return response()->json($this->productService->getProductById($id));
    }

    public function store(Request $request)
    {
        $data = $request->validate([
            'name' => 'required|string|max:255',
            'price' => 'required|numeric',
            'stock' => 'required|integer',
        ]);
        return response()->json($this->productService->createProduct($data));
    }

    public function update(Request $request, $id)
    {
        $data = $request->validate([
            'name' => 'string|max:255',
            'price' => 'numeric',
            'stock' => 'integer',
        ]);
        return response()->json($this->productService->updateProduct($id, $data));
    }

    public function destroy($id)
    {
        return response()->json($this->productService->deleteProduct($id));
    }
}

Step 7: Define API Routes

Finally, define the API routes for the ProductController.

Modify routes/api.php:

use App\Http\Controllers\ProductController;

Route::prefix('products')->group(function () {
    Route::get('/', [ProductController::class, 'index']);
    Route::get('/{id}', [ProductController::class, 'show']);
    Route::post('/', [ProductController::class, 'store']);
    Route::put('/{id}', [ProductController::class, 'update']);
    Route::delete('/{id}', [ProductController::class, 'destroy']);
});

Benefits of Using Repository and Service Pattern

  1. Decoupling: The service layer is independent of the repository implementation.

  2. Easy to Swap Implementations: You can switch to another repository (e.g., API-based repository) without changing the service or controller.

  3. Better Testing: You can mock the repository or service in unit tests.

  4. Cleaner Code: The controller is lightweight and follows SOLID principles.

Conclusion

By implementing the Repository and Service Pattern in Laravel, you can create a clean, maintainable, and scalable application. This pattern separates concerns, making your code easier to test and extend. In this guide, we walked through a simple CRUD example for managing products, but you can apply this pattern to any part of your application.

Next Steps

  • Explore advanced features like caching and pagination in the repository.

  • Implement validation and error handling in the service layer.

  • Use Dependency Injection to further decouple your application.

...

🔧 How to Use Repository and Service Pattern in Laravel


📈 35.2 Punkte
🔧 Programmierung

🔧 Use Repository Pattern with Laravel


📈 31.69 Punkte
🔧 Programmierung

🔧 Structuring a Laravel Project with the Repository Pattern and Services 🚀


📈 29.17 Punkte
🔧 Programmierung

🔧 Creating A Laravel CRUD Application With The Repository Pattern


📈 28.03 Punkte
🔧 Programmierung

🔧 Why Implement the Repository Pattern in Laravel?


📈 28.03 Punkte
🔧 Programmierung

🔧 Simplifying Data Access in Laravel with the Repository Pattern


📈 28.03 Punkte
🔧 Programmierung

🎥 Upgrade Laravel 11 to Laravel 12 #laravel #thecodeholic


📈 26.57 Punkte
🎥 Video | Youtube

🎥 Using Laravel Breeze Starter Kits in Laravel 12 #laravel #thecodeholic #laravel12


📈 26.57 Punkte
🎥 Video | Youtube

🎥 Print SQL Queries in Laravel #laravel #thecodeholic #laravelcourse #php #laravel


📈 26.57 Punkte
🎥 Video | Youtube

🎥 Create First Laravel Project using Laravel Herd #laravel #laravelproject #laravelphp


📈 26.57 Punkte
🎥 Video | Youtube

🔧 [Laravel v11 x Docker] Efficiently Set Up a Laravel App Dev Environment with Laravel Sail


📈 26.57 Punkte
🔧 Programmierung

🔧 .NET Core MVC Project Structure : Implementing a Generic Service and Repository Pattern


📈 22.68 Punkte
🔧 Programmierung

🔧 XAMPP vs. Laragon vs. Laravel Herd: Which One Should You Use for PHP and Laravel Projects?


📈 22.51 Punkte
🔧 Programmierung

🔧 @Slf4j = Facade Pattern + Service Locator Pattern


📈 22.25 Punkte
🔧 Programmierung

🔧 Service: O pattern que virou anti-pattern


📈 22.25 Punkte
🔧 Programmierung

🔧 How to Use a GitHub Private Repository as a Helm Chart Repository


📈 22.12 Punkte
🔧 Programmierung

🔧 Repository vs service design pattern


📈 21.54 Punkte
🔧 Programmierung

🔧 Implementing the Repository Pattern with Entity Framework and Dapper in a C# Application


📈 20.31 Punkte
🔧 Programmierung

🔧 The Repository and Unit of Work pattern


📈 20.31 Punkte
🔧 Programmierung

🔧 Repository Design Pattern, Highlighting Packages via ADS, and New Arabic Article ✨


📈 20.31 Punkte
🔧 Programmierung

🔧 Understanding and Implementing the Repository Pattern in .NET


📈 20.31 Punkte
🔧 Programmierung

🔧 Implementing the repository pattern in Go with both in-memory and MySQL repositories


📈 20.31 Punkte
🔧 Programmierung

🎥 Build and Deploy Real Time Messaging App with Laravel and React (with Laravel Reverb)


📈 19.99 Punkte
🎥 Video | Youtube

🔧 Singleton pattern in PHP and IOC (Inversion Of Control) container in laravel


📈 19.94 Punkte
🔧 Programmierung

🔧 [Design Pattern] Observer pattern for achievement System


📈 19.88 Punkte
🔧 Programmierung

🔧 Go program pattern 01: Functional Options Pattern


📈 19.88 Punkte
🔧 Programmierung

🔧 Implementing Chain of Responsibility Pattern in C# : Middleware's Design Pattern


📈 19.88 Punkte
🔧 Programmierung

🔧 A transição do Higher-Order Component pattern para o React Hooks pattern


📈 19.88 Punkte
🔧 Programmierung

🔧 Webkul pattern question advance pattern for interview with python


📈 19.88 Punkte
🔧 Programmierung

📰 Neu in .NET 7 [5]: List Pattern und Slice Pattern mit C# 11


📈 19.88 Punkte
📰 IT Nachrichten

🔧 Design Pattern #7 - Builder Pattern


📈 19.88 Punkte
🔧 Programmierung

🔧 C# Pattern Matching Inside Out: Kompakter und prägnanter C#-Code durch Pattern Matching


📈 19.88 Punkte
🔧 Programmierung

matomo