Let's talk about this first before starting the tutorial on the Laravel repository pattern. The first question is why do you use design patterns in your application? The simplest answer is for beautiful code structure, reusable code, easy to understand and for easy to maintain, and many more you can say. So it is important to write reusable code in your application so that you can use that code in your application any place very easily.

For this purpose, we are going to learn in this tutorial, Laravel 9 repository pattern tutorial. I will show you a complete guide on Laravel repository pattern best practices. Though Laravel follows an MVC architecture design pattern you know, now we will see how to create a service repository and create some logic by using this service repository in the Laravel application.

In this Laravel repository design pattern tutorial, I will create an example interface and then a user repository. Assume that, we fetch users, delete users and user stores and create users. But we will create a system that will solve all those crud mechanisms using repository design patterns then how cool it is?

laravel-9-repository-design-pattern

So let's start the repository Laravel 9 and the first step create an interface like that:

App\Repository\IUserRepository.php

<?php

namespace App\Repository;

interface IUserRepository 
{
    public function list();
    public function findById($id);
    public function storeOrUpdate( $id = null, $data= [] );
    public function destroyById($id);
}

 

Look at that, we defined four methods inside the interface and all of the methods do different tasks. The 'list()' method will return all the users and the 'findById()' method will return only a single user by id and the 'storeOrUpdate()' method will store or update the users and the 'destroyById()' method will destroy the single user by id.

 

Read also: Why And When To Use Interface In Laravel Application

 

Now time to create a 'UserRepository' class that implements the user interface and completes the task from here. So create this class and update it like the below:

App\Repository\UserRepository.php

<?php

namespace App\Repository;

use App\Repository\IUserRepository;

class UserRepository implements IUserRepository
{   
   
    public function list()
    {   
        //fetch all the users
    }

    public function findById($id)
    {
        //fetch single user
    }

    public function storeOrUpdate($id = null, $data = [] )
    {   
        //store or update the user
    }
    
    public function destroyById($id)
    {
        //delete user
    }
}

 

Look at that, in this implemented class, we have to complete all the tasks. So this part is called the Laravel service repository pattern. Now create a controller and use them in your controller like that:

App\Http\Controllers\UserController.php

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Repository\IUserRepository;

class UserController extends Controller
{   
    protected $user;

    public function __construct(IUserRepository $user)
    {
        $this->user = $user;
    }

    public function index()
    {   
        return $this->user->list();
    }

    public function store(Request $request)
    {   
        //store  user
        $this->user->storeOrUpdate($id = null, $data);
    }

    public function show($id)
    {   
        return $this->user->findById($id);
    }

    public function update(Request $request, $id)
    {   
        //update user
        $this->user->storeOrUpdate($id, $data);
    }

    public function destroy($id)
    {   
        //delete user
        $this->user->destroyById($id);
    }
}

 

Look at that, our interface, service repository, and controller are ready to go. Now we have to do one more thing. We have to bind the repository class to our interface before using the interface. If we do not bind then you will face an error like that:

Target [App\Repository\IUserRepository] is not instantiable while building [App\Http\Controllers\UserController].

 

So now bind it in your 'AppServiceProvider' that way:

App\Providers\AppServiceProvider.php

<?php

namespace App\Providers;

use App\Repository\UserRepository;
use App\Repository\IUserRepository;
use Illuminate\Support\ServiceProvider;

class AppServiceProvider extends ServiceProvider
{

    public function register()
    {
        $this->app->bind(IUserRepository::class, UserRepository::class);
    }

    public function boot()
    {
        
    }
}

 

Now everything is ready to go. You now know what is Laravel service repository pattern and how to implement Laravel 9 repository pattern.

 

Recommended: Central Place Of Laravel - Service Provider Explanation

 

Conclusion

Look at the code structure. How cool is this? This is the use case of the Laravel repository pattern. We have created a Laravel 9 repository pattern using the interface by binding it with the service class with this interface using the service container. Hope this Laravel repository pattern example tutorial will help you.