Skip to content

Reposiotry Pattern implemention for PHP (inc: Laravel, Symfony, ...)

License

Notifications You must be signed in to change notification settings

morilog/flexible-repository

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

12 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Laravel Flexible Repository

A Laravel package for creating Flexible and powerful repositories

Installation

composer require morilog/flexible-repository

Usage

Create Repositories

Create for Eloquent

  • Create an interface for your repository that extended from Morilog\FlexibleRepository\Contracts\RepositoryInterface
<?php
namesapce App\Reposiotries;

use Morilog\FlexibleRepository\Contracts\RepositoryInterface;

interface UserRepository extends RepositoryInterface
{
}
  • Create a class For your model that extended from Morilog\FlexibleRepository\BaseEloquentRepository and implements your declared interface
<?php
namespace App\Repositories;

use Morilog\FlexibleRepository\BaseEloquentRepository;
use App\Models\User;

class EloquentUserRepository extends BaseEloquentRepository implements UserRepository
{
    protected function model()
    {
        return User::class;
    }
}
  • Bind your Repository to implemented class
<?php
namespace App\Providers;

use Illuminate\Support\ServiceProvider;
use App\Repositories\UserRepository;
use App\Repositories\EloqeuntUserRepository;

class RepositoryServiceProvider extends ServiceProvider
{
    public function register()
    {
         $this->app->bind(UserRepository::class, function ($app) {
            return new EloquentUserRepository($app);
         });
         
        // or
        // $this->app->bind(UserRepository::class, EloquentUserRepository::class);
        
    }
}
  • Inject your repository in classes and controller methods
<?php
namespace App\Http\Controllers;

use App\Repositories\UserReposiotry;

class UsersController extends Controller
{
    public function index(UserRepository $repository)
    {
        return $repository->all();
    }
}