m2quared / Eloquent-Depot by JayBizzle

Repositories to the database layer
20,082
1
4
Package Data
Maintainer Username: JayBizzle
Maintainer Contact: m@rkbee.ch (Mark Beech)
Package Create Date: 2016-05-19
Package Last Update: 2020-11-02
Language: PHP
License: MIT
Last Refreshed: 2024-11-19 03:11:44
Package Statistics
Total Downloads: 20,082
Monthly Downloads: 2
Daily Downloads: 0
Total Stars: 1
Total Watchers: 4
Total Forks: 0
Total Open Issues: 0

Eloquent Depot

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

Table of Contents

  • Installation
    • Composer
    • Laravel

Installation

Composer

Execute the following command to get the latest version of the package:

composer require m2quared/l5-repository

Laravel

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

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

If Lumen

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

Publish Configuration

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.

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

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

Find all results in Repository with pagination

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

Find by result by id

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

Hiding attributes of the model

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

Showing only specific attributes of the model

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

Loading the Model relationships

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

Find by result by field name

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

Find by result by multiple fields

$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

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

Find by result by excluding multiple values in one field

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

Find all using custom scope

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

Create new entry in Repository

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

Update entry in Repository

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

Delete entry in Repository

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