2017 © Pedro Peláez
 

library eloquent-depot

Repositories to the database layer

image

m2quared/eloquent-depot

Repositories to the database layer

  • Wednesday, July 5, 2017
  • by JayBizzle
  • Repository
  • 3 Watchers
  • 1 Stars
  • 7,176 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 7 Versions
  • 7 % Grown

The README.md

Eloquent Depot

Eloquent Depot is used to abstract the data layer, making our application more flexible to maintain., (*1)

Table of Contents

Installation

Composer

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

composer require m2quared/l5-repository

Laravel

In your config/app.php add M2quared\Repository\Providers\RepositoryServiceProvider::class to the end of the providers array:, (*3)

'providers' => [
    ...
    M2quared\Repository\Providers\RepositoryServiceProvider::class,
],

If Lumen, (*4)

$app->register(M2quared\Repository\Providers\LumenRepositoryServiceProvider::class);

Publish Configuration, (*5)

php artisan vendor:publish

Methods

M2quared\Repository\Contracts\RepositoryInterface

  • all($columns = array('*'))
  • first($columns = array('*'))
  • paginate($limit = null, $columns = ['*'])
  • find($id, $columns = ['*'])
  • findByField($field, $value, $columns = ['*'])
  • findWhere(array $where, $columns = ['*'])
  • findWhereIn($field, array $where, $columns = [*])
  • findWhereNotIn($field, array $where, $columns = [*])
  • create(array $attributes)
  • update(array $attributes, $id)
  • updateOrCreate(array $attributes, array $values = [])
  • delete($id)
  • orderBy($column, $direction = 'asc');
  • with(array|string $relations);
  • withCount(array|string $relations)
  • limit($value)
  • hidden(array $fields);
  • visible(array $fields);
  • scopeQuery(Closure $scope);
  • getFieldsSearchable();
  • setPresenter($presenter);
  • skipPresenter($status = true);

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 Post extends Eloquent { // or Ardent, Or any other Model Class

    protected $fillable = [
        'title',
        'author',
        ...
     ];

     ...
}

Create a Repository

namespace App;

use M2quared\Repository\Eloquent\BaseRepository;

class PostRepository extends BaseRepository {

    /**
     * Specify Model class name
     *
     * @return string
     */
    function model()
    {
        return "App\\Post";
    }
}

Use methods

namespace App\Http\Controllers;

use App\PostRepository;

class PostsController extends BaseController {

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

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

    ....
}

Find all results in Repository, (*7)

$posts = $this->repository->all();

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

$posts = $this->repository->paginate($limit = null, $columns = ['*']);

Find by result by id, (*9)

$post = $this->repository->find($id);

Hiding attributes of the model, (*10)

$post = $this->repository->hidden(['country_id'])->find($id);

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

$post = $this->repository->visible(['id', 'state_id'])->find($id);

Loading the Model relationships, (*12)

$post = $this->repository->with(['state'])->find($id);

Find by result by field name, (*13)

$posts = $this->repository->findByField('country_id','15');

Find by result by multiple fields, (*14)

$posts = $this->repository->findWhere([
    //Default Condition =
    'state_id'=>'10',
    'country_id'=>'15',
    //Custom Condition
    ['columnName','>','10']
]);

Find by result by multiple values in one field, (*15)

$posts = $this->repository->findWhereIn('id', [1,2,3,4,5]);

Find by result by excluding multiple values in one field, (*16)

$posts = $this->repository->findWhereNotIn('id', [6,7,8,9,10]);

Find all using custom scope, (*17)

$posts = $this->repository->scopeQuery(function($query){
    return $query->orderBy('sort_order','asc');
})->all();

Create new entry in Repository, (*18)

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

Update entry in Repository, (*19)

$post = $this->repository->update( Input::all(), $id );

Delete entry in Repository, (*20)

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

The Versions