Home Arrow Icon Knowledge base Arrow Icon Global Arrow Icon How do I inject a repository using dependency injection in Laravel 11


How do I inject a repository using dependency injection in Laravel 11


Here is how you can inject a repository using dependency injection in a Laravel 11 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.

Citations:
[1] https://dev.to/iamkirillart/how-to-implement-dependency-injection-in-laravel-livewire-con
[2] https://www.linkedin.com/pulse/working-laravel-repository-pattern-kais-chrif-nacvf
[3] https://laracasts.com/discuss/channels/laravel/injecting-a-repository-into-another-repository-class
[4] https://stackoverflow.com/questions/41282101/is-it-possible-to-inject-a-repository-as-a-dependency-in-a-laravel-service-file
[5] https://laracasts.com/index.php/discuss/channels/laravel/repositories-and-dependency-injection?reply=377169