2017 © Pedro Peláez
 

library lara-repo

Repository pattern for Laravel

image

omnicode/lara-repo

Repository pattern for Laravel

  • Wednesday, July 18, 2018
  • by omnicode
  • Repository
  • 1 Watchers
  • 2 Stars
  • 717 Installations
  • PHP
  • 3 Dependents
  • 3 Suggesters
  • 1 Forks
  • 0 Open issues
  • 45 Versions
  • 65 % Grown

The README.md

Build Status Total Downloads Latest Stable Version License , (*1)

Installation

Run the following command from you terminal:, (*2)

bash composer require "omnicode/lara-repo: 2.0.*", (*3)

or add this to require section in your composer.json file:, (*4)

"omnicode/lara-repo": "2.0.*", (*5)

then run composer update, (*6)

Usage

First, create your RepositoryInterface interface that should extend LaraRepo\Contracts\RepositoryInterface, (*7)

e.g., (*8)

<?php

namespace App\Repositories\Contracts;

use LaraRepo\Contracts\RepositoryInterface;

interface AccountRepositoryInterface extends RepositoryInterface
{

}

Next, create your repository class. Note that your repository class should extend LaraRepo\Eloquent\AbstractRepository and have modelClass() method, (*9)

e.g., (*10)

<?php

namespace App\Repositories\Eloquent;

use LaraRepo\Eloquent\AbstractRepository;
use App\Repositories\Contracts\AccountRepositoryInterface;
use App\Models\Account;

class AccountRepository extends AbstractRepository  implements AccountRepositoryInterface
{
    public function modelClass()
    {
        return Account::class;
    }
}

modelClass() method is used to identify the model class, (*11)

It is suggested to use LaraModel repository to utilize all the functionality, (*12)

"omnicode/lara-model": "2.0.*" and use, (*13)

And you can create App\Models\Account model, (*14)

<?php

namespace App\Models;

use LaraModel\Models\LaraModel;

class Account extends LaraModel
{

}

Next Bind it in ServiceProvider., (*15)

<?php

namespace App\Providers;

use App\Repositories\Contracts\AccountUserRepositoryInterface;
use App\Repositories\Eloquent\AccountRepository;
use Illuminate\Support\ServiceProvider;

class AppServiceProvider extends ServiceProvider
{
    /**
     * Register any application services.
     *
     * @return void
     */
    public function register()
    {
        $this->app->bind(AccountUserRepositoryInterface::class, AccountRepository::class);
    }
}

And finally, use the repository in your controller or service layer, (*16)

<?php

namespace App\Http\Controllers;

use App\Repositories\Contracts\AccountRepositoryInterface as AccountRepository;

class AccountsController extends Controller
{

    /**
     * @var AccountRepository
     */
    protected $accountRepository;

    /**
     * AccountsController constructor.
     * @param AccountRepository $accountRepository
     */
    public function __construct(AccountRepository $accountRepository)
    {
        $this->accountRepository = $accountRepository;
    }

    /**
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function index()
    {
        $items = $this->accountRepository->all(); 
        return view('accounts.index', compact('items'));
    }
}

Available Methods

The following methods are available:, (*17)

LaraRepo\Contracts\RepositoryInterface
    public function makeModel();
    public function getModel();
    public function getModelQuery();
    public function resetModelQuery();
    public function getTable();
    public function getKeyName();
    public function fixColumns($columns, $table = null, $prefix = null);
    public function getFillableColumns();
    public function getIndexableColumns($full = false, $hidden = true, $group = self::GROUP);
    public function getShowableColumns($full = false, $hidden = true, $group = self::GROUP);
    public function getSearchableColumns();
    public function getListableColumns();
    public function getSortableColumns($column = null, $group = self::GROUP);
    public function getStatusColumn();
    public function setSortingOptions($column = null, $order = 'asc', $group = self::GROUP);
    public function getRelations();
    public function saveAssociated($data, $options = [], $model = null);
    public function create(array $data);
    public function createWith(array $data, $attribute, $value);
    public function increment($column, $value);
    public function update(array $data, $id, $attribute = "id");
    public function updateBased(array $data, array $conditions);
    public function destroy($id);
    public function destroyBy($attribute, $value);
    public function all($columns = null);
    public function first($columns = null);
    public function last($columns = null);
    public function find($id, $columns = null);
    public function findForShow($id, $columns = null);
    public function findBy($attribute, $value, $columns = null);
    public function findAllBy($attribute, $value, $columns = null);
    public function findAttribute($id, $attribute);
    public function findFillable($id);
    public function findAllFillable($attribute, $value);
    public function findFillableWith($id, $related = []);
    public function findFillableWhere($id, $field, $value, $cmp = '=');
    public function findList($active = true, $listable = null);
    public function findListBy($attribute, $value, $active = true, $listable = null);
    public function paginate($perPage = 15, $columns = null, $group = self::GROUP);
    public function paginateWhere($attribute = '', $value = '', $cmp = '=');
    public function findCount($attribute = null, $value = null, $cmp = '=');
    public function exists($id);
    public function existsWhere($attribute, $value);
}
LaraRepo\Contracts\CriteriaInterface
    public function getCriteria();
    public function getByCriteria(Criteria $criteria);
    public function resetScope();
    public function pushCriteria(Criteria $criteria);
    public function skipCriteria($status = true);
    public function applyCriteria();
LaraRepo\Contracts\TransactionInterface
    public function startTransaction();
    public function commitTransaction();
    public function rollbackTransaction();

Example usage

Create a new account repository:, (*18)

e.g., (*19)


$this->accountRepository->create($request->all()); $this->accountRepository->createWith($request->all(), 'name', $name); it is equivalent $data = array_merge($request->all(), ['name' => $name]) $this->accountRepository->create($data); //if your model extends LaraModel/Models/LaraModel you can use saveAssociated Method for saving new account //or update existing model with relations $this->accountRepository->saveAssociated($data, $relations); //for updating with relations $this->accountRepository->saveAssociated($data, $relations, $account);

Update existing account:, (*20)

    // update based on id
    $this->accountRepository->update($request->all(), $accountId);

    // update based on attribute
    $this->accountRepository->update($request->all(), $attribute, 'attribute');

    // for increment attribute with value
    $this->accountRepository->increment($attribute, $value);

    //for decrement attribute with value
    $this->accountRepository->decrement($attribute, $value);

Delete account:, (*21)

    // delete by id
    $this->accountRepository->destroy($accountId);

Find account(s) and specify columns with you want to fetch, (*22)

    // find by accountId
    $this->accountRepository->find($accountId, $columns);

    // find by attribute (first occurence)
    $this->accountRepository->findBy($attribute, $value, $columns);

    // find by attribute (all occurence)
    $this->accountRepository->findAllBy($attribute, $value, $columns);

    // to find and return exact attribute 
    $this->accountRepository->findAttribute($accountId, $attribute);

    // to find account by accountId with fillable columns
    $this->accountRepository->findFillable($accountId);

    // to find account by attribute with fillable columns
    $this->accountRepository->findFillableWhere($id, $attribute, $value, $cmp = '=')

    // to find all accounts by attribute with fillable columns use
    $this->accountRepository->findAllFillable($attribute, $value);

    // to find account with relations
    $this->accountRepository->findFillableWith($id, $related = [])


    // to find all accounts
    $this->accountRepository->all($columns)

    // to find first account
    $this->accountRepository->first($columns = null)

    //for find last account
    $this->accountRepository->last($columns = null)

    // if your model extends LaraModel\Models\LaraModel
    // and $showable property is specified you can use
    $this->accountRepository->findForShow($id, $columns = null)

    // if your model extends LaraModel\Models\LaraModel
    // and specifed $listable property you can use
    // for find accounts list 
    //[
    //      1 => 'account1',
    //      2 => 'account2'
    //]
    //
    $this->accountRepository->findList($active = true, $listable = null)

    // or you can use to find by attribute
    $this->accountRepository->findListBy($attribute, $value, $active = true, $listable = null)


    // to check if account exists with id
    $this->accountRepository->exists($id);

    // to find count of rows matching the given critera
    $this->accountRepository->findCount($attribute, $value, $cmp);

    // to check if item exists with given attribute and value
    $this->accountRepository->existsWhere($attribute, $value);

    // for pagination
    $this->accountRepository->paginate();

    // for pagination based by condition
    $this->accountRepository->paginateWhere($attribute, $value);

model related methods, (*23)


// to get the binded model $this->account->getModel(); // get current model query $this->account->getModelQuery(); // to reset model query $this->account->resetModelQuery(); // to get model's table name $this->account->getTable(); // to get model table's primary key $this->account->getKeyName(); // to get model's fillable columns $this->account->getFillableColumns();

if your model extends LaraModel\Models\LaraModel you can also use this methods, (*24)

    // returns columns including table name
    $this->accountRepository->fixColumns($columns, $table = null, $prefix = null);

    // returns indexable columns
    $this->account->getIndexableColumns($full = false, $hidden = true, $group = self::GROUP);

    // returns columns based on $showable property
    $this->accountRepository->getShowableColumns($full = false, $hidden = true, $group = self::GROUP);

    // returns serchable columns
    $this->accountRepository->getSearchableColumns();

    // returns listable columns
    $this->accountRepository->getListableColumns();

    // returns sortable columns
    $this->accountRepository->getSortableColumns($column = null, $group = self::GROUP);

    // returns status column
    $this->accountRepository->getStatusColumn();

    // add sorting options in columns
    $this->accountRepository->setSortingOptions($column = null, $order = 'asc', $group = self::GROUP);

    // returns model relations
    $this->accountRepository->getRelations();

Criteria methods, (*25)


$this->accountRepository->getCriteria(); $this->accountRepository->getByCriteria(Criteria $criteria); $this->accountRepository->resetScope(); $this->accountRepository->pushCriteria(Criteria $criteria); $this->accountRepository->skipCriteria($status = true); $this->accountRepository->applyCriteria();

Transaction methods, (*26)


$this->accountRepository->startTransaction(); $this->accountRepository->commitTransaction(); $this->accountRepository->rollbackTransaction();
LaraRepo\Contracts\Criteria\Criteria
    public abstract function apply($modelQuery, RepositoryInterface $repository);

Criteria

Criteria is a simple way to apply specific condition, or set of conditions to the query, (*27)

Available Criteria

The following criteria are available:, (*28)

    LaraRepo\Criteria\Distinct\DistinctCriteria
    LaraRepo\Criteria\Group\GroupByCriteria             __construct($columns)
    LaraRepo\Criteria\Has\HasCriteria                   __construct($columns, $value, $cmp = '=')
    LaraRepo\Criteria\Join\InnerJoinCriteria            __construct($relation, $column = '', $values = [])
    LaraRepo\Criteria\Join\InnerJoinRelationCriteria    __construct($relation, $otherKeys = [])
    LaraRepo\Criteria\Join\LeftJoinCriteria             __construct($relation, $column = '', $values = [])
    LaraRepo\Criteria\Limit\LimitCriteria               __construct($limit)
    LaraRepo\Criteria\Offset\OffsetCriteria             __construct($offset)
    LaraRepo\Criteria\Order\SortCriteria                __construct($column, $order = 'asc', $fixColumns = true)
    LaraRepo\Criteria\Search\SearchCriteria             __construct($value, $columns = null, $table = null)
    LaraRepo\Criteria\Select\SelectCriteria             __construct($columns = [], $table = null)
    LaraRepo\Criteria\Select\SelectFillableCriteria     __construct($include = [], $exclude = [])             
    LaraRepo\Criteria\Select\SelectCriteria             __construct($relation, $columns, $prefix = true)
    LaraRepo\Criteria\Where\ActiveCriteria
    LaraRepo\Criteria\Where\BetweenCriteria             __construct($column, $from, $to)
    LaraRepo\Criteria\Where\OrWhereCriteria             __construct($attribute, $value, $cmp = '=') 
    LaraRepo\Criteria\Where\WhereCriteria               __construct($attribute, $value, $cmp = '=')
    LaraRepo\Criteria\Where\WhereHasRelationCriteria    __construct($relation, $where = [])
    LaraRepo\Criteria\Where\WhereInCriteria             __construct($attribute, $values = [])
    LaraRepo\Criteria\Where\WhereRelationCriteria       __construct($relation, $attribute, $value, $cmp = '=')
    LaraRepo\Criteria\With\RelationCriteria             __construct($relations = [])
    LaraRepo\Criteria\With\WithCountCriteria             __construct($relations)

You can make your own Criteria. Your criteria class MUST extend the abstract LaraRepo\Criteria\Criteria class., (*29)

For exapmle:, (*30)

<?php
namespace YourNamespace;

use LaraRepo\Contracts\RepositoryInterface;
use LaraRepo\Criteria\Criteria;

class NewCriteria extends Criteria
{

    public function __construct()
    {
        //your code
    }

    /***
     * @param $modelQuery
     * @param RepositoryInterface $repository
     * @return mixed
     */
    public function apply($modelQuery, RepositoryInterface $repository)
    {
        //your code here
        return $modelQuery;
    }

}

Now, inside you controller class you call pushCriteria method:, (*31)

<?php

namespace App\Http\Controllers;

use LaraRepo\Criteria\Where\WhereCriteria;
use App\Repositories\Contracts\AccountRepositoryInterface as AccountRepository;

class AccountsController extends Controller
{

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

    /**
     * AccountsController constructor.
     * @param AccountRepository $accountRepository
     */
    public function __construct(AccountRepository $accountRepository)
    {
        $this->repository = $accountRepository;
    }

    /**
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function index()
    {
        $this->repository->pushCriteria(new WhereCriteria('id', '15', '>'));
        $items = $this->repository->all(); 
        return view('accounts.index', compact('items'));
    }
}

Credits

This package is largely inspired by this great package by @andersao, (*32)

The Versions

18/07 2018

v3.x-dev

3.9999999.9999999.9999999-dev

Repository pattern for Laravel

  Sources   Download

MIT

The Requires

 

The Development Requires

by Avatar omnicode

laravel repository

18/07 2018

3.0.3

3.0.3.0

Repository pattern for Laravel

  Sources   Download

MIT

The Requires

 

The Development Requires

by Avatar omnicode

laravel repository

09/07 2018

3.0.2

3.0.2.0

Repository pattern for Laravel

  Sources   Download

MIT

The Requires

 

The Development Requires

by Avatar omnicode

laravel repository

19/06 2018

3.0.1

3.0.1.0

Repository pattern for Laravel

  Sources   Download

MIT

The Requires

 

The Development Requires

by Avatar omnicode

laravel repository

07/06 2018

v1.x-dev

1.9999999.9999999.9999999-dev

Repository pattern for Laravel

  Sources   Download

MIT

The Requires

 

The Development Requires

by Avatar omnicode

laravel repository

07/06 2018

2.0.10

2.0.10.0

Repository pattern for Laravel

  Sources   Download

MIT

The Requires

 

The Development Requires

by Avatar omnicode

laravel repository

07/06 2018

2.0.9

2.0.9.0

Repository pattern for Laravel

  Sources   Download

MIT

The Requires

 

The Development Requires

by Avatar omnicode

laravel repository

06/06 2018

2.0.8

2.0.8.0

Repository pattern for Laravel

  Sources   Download

MIT

The Requires

 

The Development Requires

by Avatar omnicode

laravel repository

06/06 2018

2.0.7

2.0.7.0

Repository pattern for Laravel

  Sources   Download

MIT

The Requires

 

The Development Requires

by Avatar omnicode

laravel repository

04/06 2018

3.0.0

3.0.0.0

Repository pattern for Laravel

  Sources   Download

MIT

The Requires

 

The Development Requires

by Avatar omnicode

laravel repository

04/04 2018

dev-master

9999999-dev

Repository pattern for Laravel

  Sources   Download

MIT

The Requires

 

The Development Requires

by Avatar omnicode

laravel repository

04/04 2018

2.0.6

2.0.6.0

Repository pattern for Laravel

  Sources   Download

MIT

The Requires

 

The Development Requires

by Avatar omnicode

laravel repository

02/03 2018

2.0.5

2.0.5.0

Repository pattern for Laravel

  Sources   Download

MIT

The Requires

 

The Development Requires

by Avatar omnicode

laravel repository

02/03 2018

2.0.4

2.0.4.0

Repository pattern for Laravel

  Sources   Download

MIT

The Requires

 

The Development Requires

by Avatar omnicode

laravel repository

26/02 2018

2.0.3

2.0.3.0

Repository pattern for Laravel

  Sources   Download

MIT

The Requires

 

The Development Requires

by Avatar omnicode

laravel repository

24/02 2018

2.0.2

2.0.2.0

Repository pattern for Laravel

  Sources   Download

MIT

The Requires

 

The Development Requires

by Avatar omnicode

laravel repository

24/02 2018

v0.x-dev

0.9999999.9999999.9999999-dev

Repository pattern for Laravel

  Sources   Download

MIT

The Requires

 

The Development Requires

by Avatar omnicode

laravel repository

23/02 2018

1.0.0

1.0.0.0

Repository pattern for Laravel

  Sources   Download

MIT

The Requires

 

The Development Requires

by Avatar omnicode

laravel repository

22/02 2018

2.0.1

2.0.1.0

Repository pattern for Laravel

  Sources   Download

MIT

The Requires

 

The Development Requires

by Avatar omnicode

laravel repository

22/02 2018

2.0.0

2.0.0.0

Repository pattern for Laravel

  Sources   Download

MIT

The Requires

 

The Development Requires

by Avatar omnicode

laravel repository

19/02 2018

0.1.1

0.1.1.0

Repository pattern for Laravel

  Sources   Download

MIT

The Requires

 

The Development Requires

by Avatar omnicode

laravel repository

13/02 2018

0.1.0

0.1.0.0

Repository pattern for Laravel

  Sources   Download

MIT

The Requires

 

The Development Requires

by Avatar omnicode

laravel repository

13/02 2018

0.0.23

0.0.23.0

Repository pattern for Laravel

  Sources   Download

MIT

The Requires

 

The Development Requires

by Avatar omnicode

laravel repository

26/01 2018

0.0.22

0.0.22.0

Repository pattern for Laravel

  Sources   Download

MIT

The Requires

 

The Development Requires

by Avatar omnicode

laravel repository

11/12 2017

0.0.21

0.0.21.0

Repository pattern for Laravel

  Sources   Download

MIT

The Requires

 

The Development Requires

by Avatar omnicode

laravel repository

14/11 2017

0.0.20

0.0.20.0

Repository pattern for Laravel

  Sources   Download

MIT

The Requires

 

The Development Requires

by Avatar omnicode

laravel repository

14/11 2017

0.0.19

0.0.19.0

Repository pattern for Laravel

  Sources   Download

MIT

The Requires

 

The Development Requires

by Avatar omnicode

laravel repository

18/10 2017

0.0.17

0.0.17.0

Repository pattern for Laravel

  Sources   Download

MIT

The Requires

 

The Development Requires

by Avatar omnicode

laravel repository

18/10 2017

0.0.18

0.0.18.0

Repository pattern for Laravel

  Sources   Download

MIT

The Requires

 

The Development Requires

by Avatar omnicode

laravel repository

18/10 2017

0.0.16

0.0.16.0

Repository pattern for Laravel

  Sources   Download

MIT

The Requires

 

The Development Requires

by Avatar omnicode

laravel repository

06/10 2017

0.0.15

0.0.15.0

Repository pattern for Laravel

  Sources   Download

MIT

The Requires

 

The Development Requires

by Avatar omnicode

laravel repository

24/09 2017

0.0.14

0.0.14.0

Repository pattern for Laravel

  Sources   Download

MIT

The Requires

 

The Development Requires

by Avatar omnicode

laravel repository

23/09 2017

0.0.13

0.0.13.0

Repository pattern for Laravel

  Sources   Download

MIT

The Requires

 

The Development Requires

by Avatar omnicode

laravel repository

23/09 2017

0.0.12

0.0.12.0

Repository pattern for Laravel

  Sources   Download

MIT

The Requires

 

The Development Requires

by Avatar omnicode

laravel repository

23/09 2017

0.0.11

0.0.11.0

Repository pattern for Laravel

  Sources   Download

MIT

The Requires

 

The Development Requires

by Avatar omnicode

laravel repository

21/09 2017

0.0.10

0.0.10.0

Repository pattern for Laravel

  Sources   Download

MIT

The Requires

 

The Development Requires

by Avatar omnicode

laravel repository

20/09 2017

0.0.9

0.0.9.0

Repository pattern for Laravel

  Sources   Download

MIT

The Requires

 

The Development Requires

by Avatar omnicode

laravel repository

19/09 2017

0.0.8

0.0.8.0

Repository pattern for Laravel

  Sources   Download

MIT

The Requires

 

The Development Requires

by Avatar omnicode

laravel repository

14/09 2017

0.0.6

0.0.6.0

Repository pattern for Laravel

  Sources   Download

MIT

The Requires

 

The Development Requires

by Avatar omnicode

laravel repository

14/09 2017

0.0.7

0.0.7.0

Repository pattern for Laravel

  Sources   Download

MIT

The Requires

 

The Development Requires

by Avatar omnicode

laravel repository

07/09 2017

0.0.5

0.0.5.0

Repository pattern for Laravel

  Sources   Download

MIT

The Requires

 

The Development Requires

by Avatar omnicode

laravel repository

19/08 2017

0.0.4

0.0.4.0

Repository pattern for Laravel

  Sources   Download

MIT

The Requires

 

The Development Requires

by Avatar omnicode

laravel repository

02/07 2017

0.0.3

0.0.3.0

Repository pattern for Laravel

  Sources   Download

MIT

The Requires

 

The Development Requires

by Avatar omnicode

laravel repository

02/07 2017

0.0.2

0.0.2.0

Repository pattern for Laravel

  Sources   Download

MIT

The Requires

 

The Development Requires

by Avatar omnicode

laravel repository

02/07 2017

0.0.1

0.0.1.0

Repository pattern for Laravel

  Sources   Download

MIT

The Requires

 

The Development Requires

by Avatar omnicode

laravel repository