2017 © Pedro Peláez
 

library eloquent-abstract-repository

laravel eloquent abstract repository to implement repository pattern in easy way

image

ra3oul/eloquent-abstract-repository

laravel eloquent abstract repository to implement repository pattern in easy way

  • Wednesday, June 15, 2016
  • by ra3oul
  • Repository
  • 2 Watchers
  • 13 Stars
  • 94 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 2 Forks
  • 0 Open issues
  • 2 Versions
  • 21 % Grown

The README.md

Laravel 5 Eloquent Abstract repository

using repository pattern in laravel with a great base repository, (*1)

Table of Contents

Installation

Composer

Execute the following command to get the latest version of the package:, (*4)

composer require ra3oul/eloquent-abstract-repository -dev

Laravel

In your config/app.php add ra3oul\EloquentAbstractRepository\EloquentAbstractRepositoryServiceProvider::class to the end of the providers array:, (*5)

'providers' => [
    ...

    ra3oul\EloquentAbstractRepository\EloquentAbstractRepositoryServiceProvider::class,
],

Methods

ra3oul\EloquentAbstractRepository\repository\RepositoryInterface

  • create($columns = array('*'))
  • findOneById($id )
  • findOneBy($key , $value )
  • findManyBy($key,$value])
  • findManyByIds($ids = array())
  • findAll()
  • findManyByCredentials($credentials = array())
  • paginateBy($key, $value, $perPage = 10)
  • paginate($perPage = 10)
  • paginateByCredentials(array $credentials, $perPage = 10)
  • updateOneById($id, array $data = [])
  • updateOneBy($key, $value, array $data = [])
  • updateOneByCredentials(array $credentials, array $data = []');
  • updateManyBy($key, $value, array $data = []);
  • updateManyByCredentials(array $credentials = [], array $data = []);
  • updateManyByIds(array $ids, array $data = []);
  • function deleteOneById($id);
  • public function allExist(array $ids);
  • deleteOneBy($key, $value);
  • deleteOneByCredentials(array $credentials = []);
  • deleteManyBy($key, $value);
  • deleteManyByCredentials(array $credentials = []);
  • deleteManyByIds(array $ids);
  • searchByCredentials(array $credentials = [], $perPage);
  • with(array $with = []);
  • columns(array $columns = ['*']);
  • limit($limit = 10);
  • orderBy($orderBy, $sort = 'DESC');

Usage

Create a Model

Create your model normally, but it is important to define the attributes that can be filled from the input form data., (*6)

namespace App;

class Article extends Eloquent { // can be any other class name
    protected $fillable = [
        'name',
        'author',
        ...
     ];

     ...
}

Create a RepositoryInteface

namespace App;
use Foo ;
use ra3oul\EloquentAbstractRepository\repository\RepositoryInterface;

interface ArticleRepositoryInterface extends RepositoryInterface
{

}

Create a Repository

namespace App;
use Foo ;
class ArticleRepository extends AbstractEloquentRepository implements ArticleRepositoryInterface


       public function __construct(Foo $model)
    {
        parent::__construct($model);
    }
}

Create Service Provider

in order to bind interfaces to repository classes we should create a simple service provider to bind them, (*7)

namespace App\Providers;

use Illuminate\Support\ServiceProvider;

class RepositoryServiceProvider extends ServiceProvider
{
    protected function registeredRepositories()
    {
        // 'Repository Interface' => 'Implementation'
        return [
      '\App\ArticleRepositoryInterface' =>
                '\App\ArticleRepository',
                // you should add other interfaces and their implemented classes below !
        ];
    }

    /**
     * Register the service provider.
     *
     * @return void
     */
    public function register()
    {
        $repos = $this->registeredRepositories();

        foreach ($repos as $interface => $implemented) {
            $this->app->bind($interface, $implemented);
        }
    }

Use methods

namespace App\Http\Controllers;

use App\ArticleRepositoryInterface;

class ArticlesController extends BaseController {

    /**
     * @var ArticleRepository
     */
    protected $repository;

    public function __construct(FooRepositoryInterface $repository){
        $this->repository = $repository;
    }

    ....
}

Find all results in Repository, (*8)

$articles = $this->repository->findAll();

Find all results in Repository with pagination, (*9)

$aritcles = $this->repository->columns([*])->paginate($limit=10);

Find by result by id, (*10)

$articles = $this->repository->findOneById($id);

Showing only specific attributes of the model, (*11)

$article = $this->repository->columns(['id', 'state_id'])->findOneById($id);

Loading the Model relationships, (*12)

$article = $this->repository->with(['state'])->findOneById($id);

Find by result by field name, (*13)

$articles = $this->repository->findOneBy('country_id','15');

Find by result by field, (*14)


$articles = $this->repository->findManyBy('name','rasoul');

Find by result by multiple values in id, (*15)

$articles = $this->repository->findManyByIds([1,2,3,4,5]);

Create new entry in Repository, (*16)

$article = $this->repository->create( Input::all() );

Update entry in Repository, (*17)

$article = $this->repository->updateOneById(  $id , Input::all());

Delete entry in Repository, (*18)

$this->repository->deleteOneById($id)

The Versions

15/06 2016

dev-master

9999999-dev

laravel eloquent abstract repository to implement repository pattern in easy way

  Sources   Download

MIT

by rasoul abdollahi

15/06 2016

1.0

1.0.0.0

laravel eloquent abstract repository to implement repository pattern in easy way

  Sources   Download

MIT

by rasoul abdollahi