morilog / flexible-repository
Flexible and powerful repositories for laravel framework
Installs: 2 362
Dependents: 0
Suggesters: 0
Security: 0
Stars: 3
Watchers: 2
Forks: 1
Open Issues: 0
Requires
- illuminate/database: >=5.1
- illuminate/pagination: >=5.1
- illuminate/support: >=5.1
Requires (Dev)
- phpunit/phpunit: ~4.0
README
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 fromMorilog\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 declaredinterface
<?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(); } }