Home Arrow Icon Knowledge base Arrow Icon Global Arrow Icon How do I set up a repository pattern in Laravel 11 for a CRUD application
    Laravel Service Provider


How do I set up a repository pattern in Laravel 11 for a CRUD application


Here is a concise example of how to implement the Repository Pattern in a Laravel 11 application for a CRUD application:

Create the Repository Interface

First, create a repository interface that defines the methods your repository should implement. This will be used to decouple your application logic from the data access layer. In the `app/Repositories` directory, create a file called `UserRepositoryInterface.php` with the following contents:

php
namespace App\Repositories;

use App\Models\User;
use Illuminate\Database\Eloquent\Collection;

interface UserRepositoryInterface
{
    public function all(): Collection;
    public function find(int $id): ?User;
    public function create(array $data): User;
    public function update(int $id, array $data): bool;
    public function delete(int $id): bool;
}

Implement the Repository

Next, create the concrete implementation of the repository interface. In the same `app/Repositories` directory, create a file called `UserRepository.php` with the following contents:

php
namespace App\Repositories;

use App\Models\User;
use Illuminate\Database\Eloquent\Collection;

class UserRepository implements UserRepositoryInterface
{
    public function all(): Collection
    {
        return User::all();
    }

    public function find(int $id): ?User
    {
        return User::find($id);
    }

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

    public function update(int $id, array $data): bool
    {
        $user = $this->find($id);
        if (!$user) {
            return false;
        }
        return $user->update($data);
    }

    public function delete(int $id): bool
    {
        $user = $this->find($id);
        if (!$user) {
            return false;
        }
        return $user->delete();
    }
}

Bind the Repository in the Service Container

Finally, you need to bind the repository interface to the concrete implementation in the service container. You can do this in the `AppServiceProvider` by adding the following to the `register()` method:

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

Use the Repository in Your Controller

Now, you can use the repository in your controller by type-hinting the `UserRepositoryInterface` in the constructor:

php
namespace App\Http\Controllers;

use App\Repositories\UserRepositoryInterface;

class UserController extends Controller
{
    private $userRepository;

    public function __construct(UserRepositoryInterface $userRepository)
    {
        $this->userRepository = $userRepository;
    }

    public function index()
    {
        $users = $this->userRepository->all();
        return view('users.index', compact('users'));
    }

    public function show($id)
    {
        $user = $this->userRepository->find($id);
        return view('users.show', compact('user'));
    }

    // Other CRUD methods...
}

By using the repository pattern, you've decoupled your application logic from the data access layer, making your code more maintainable and testable. The controller now only knows about the repository interface, not the concrete implementation, which follows the Dependency Inversion Principle[1][3].

Citations:
[1] https://www.youtube.com/watch?v=rDOlayAw4VY
[2] https://github.com/manchenkoff/laravel-repositories
[3] https://blog.devgenius.io/laravel-repository-design-pattern-a-quick-demonstration-5698d7ce7e1f?gi=72ec02d6bec8
[4] https://stackoverflow.com/questions/57052656/how-to-implement-repository-pattern-in-laravel
[5] https://www.linkedin.com/pulse/implementing-repository-pattern-laravel-application-charles-rhglf