Repository Interface

Category : Laravel Package

A Laravel package that leverages dependency injection to cleanly separate business logic from other application layers, promoting better code organization, testability, and adherence to SOLID principles.

Installation

Install the package via Composer:

composer require vivek-mistry/repository-interface

Particular Model Generare the Repository-Interface

php app:make-repo {ModelName}

So using above two files created at app/Repositories/Interface & app/Repositories/Repository,

  • ModelNameInterface.php
  • ModelNameInterfaceRepository.php

Create Service Provider

php artisan make:provider RepositoryServiceProvider

Register Your Service Provider & register your repository

=> Register your service provider

=> In your RepositoryServiceProvider add below :

public function boot(): void  
{
    
$this->app->bind(      
INTERFACE_NAME::class,      
REPOSITORY_NAME::class    
);  
}

HOW TO USE IN YOUR CONTROLLER?

For example :

class UserController extends Controller
{
protected $userRepository;

/**
* Load Repository
*/
public function __construct(
UserRepositoryInterface $userRepository
)
{
$this->userRepository = $userRepository;
}

public function index($request)
{
$data = [
'name' => $request->name,
'email' => $request->email
];
$this->userRepository->createOrUpdate($data);
}
}


For more detail visit at Packagist