Home

Awesome

Repository Design Pattern for Laravel

StyleCI Build Status Total Downloads Latest Stable Version Latest Unstable Version License Monthly Downloads Daily Downloads Scrutinizer Code Quality Code Coverage

Install

To get the latest version of Laravel Exceptions, simply require the project using Composer:

composer require recca0120/repository

Instead, you may of course manually update your require block and run composer update if you so choose:

{
    "require": {
        "recca0120/repository": "~2.0.0"
    }
}

Methods

Recca0120\Repository\EloquentRepository

Recca0120\Repository\Criteria

Usage

Eloquent

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;

use Illuminate\Database\Eloquent\Model;

class Post extends Model
{
    protected $fillable = [
        'title',
        'author',
     ];
}

Create a Contract


namespace App\Repositories\Contracts;

interface PostRepository
{

}

Create a Repository


namespace App\Repositories;

use App\Repositories\Contracts\PostRepository as PostRepositoryContract;
use App\Post;
use Recca0120\Repository\EloquentRepository;

class PostRepository extends EloquentRepository implements PostRepositoryContract
{
    public function __construct(Post $model)
    {
        $this->model = $model;
    }
}

Bind


namespace App\Providers;

use Illuminate\Support\ServiceProvider;
use App\Repositories\Contracts\PostRepository as PostRepositoryContract;

class AppServiceProvider extends ServiceProvider
{
    public function register()
    {
        $this->app->singleton(PostRepositoryContract::class, PostRepository::class);
    }
}

Controller


namespace App\Http\Controllers;

use App\Repositories\Contracts\PostRepository;

class PostsController extends Controller
{
    protected $repository;

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

Methods

Find all results in Repository

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

Find all results in Repository with pagination

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

Count results in Repository

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

Create new entry in Repository

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

Update entry in Repository

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

Delete entry in Repository

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

New instance

$post = $this->repository->newInstance([
    'author' => 'author'
]);

Return Model With Conditions

$model = $this->repository->matching(Criteria::create()->where('title', '=', 'title'));

Find result by id

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

Find by conditions

Using the Criteria

Criteria is support all of Eloquent functions

Single Criteria

use Recca0120\Repository\Criteria;

$criteria = Criteria::create()
    ->select('*')
    ->where('author', '=', 'author')
    ->orWhere('title', '=', 'title')
    ->orderBy('author', 'asc');

$this->repository->get($criteria);
$this->repository->paginate($criteria);

Multiple Criteria


use Recca0120\Repository\Criteria;

$criteria = [];

$criteria[] = Criteria::create()
    ->orderBy('author', 'asc');

$criteria[] = Criteria::create()
    ->where('author', '=', 'author')
    ->orWhere('title', '=', 'title');

$this->repository->get($criteria);
// $this->repository->paginate($criteria);
With

use Recca0120\Repository\Criteria;

$criteria = Criteria::create()
    ->with('author', function($criteria) {
        $criteria->where('author', 'author');
    });

$this->repository->get($criteria);
// $this->repository->paginate($criteria);

Join


use Recca0120\Repository\Criteria;

$criteria = Criteria::create()
    ->join('author', function ($criteria) {
        $criteria->on('posts.author_id', '=', 'author.id');
    });

$this->repository->get($criteria);
// $this->repository->paginate($criteria);

Expression


use Recca0120\Repository\Criteria;

$criteria = Criteria::create()
    ->where('created_at', '<=', Criteria::expr('NOW()'));

$this->repository->get($criteria);
// $this->repository->paginate($criteria);

Custom Criteria


use Recca0120\Repository\Criteria;

class CustomCriteria extends Criteria
{
    public function __construct($id)
    {
        $this->where('id', '=', $id);
    }
}

$this->repository->get((new CustomCriteria(1))->where('autor', 'autor'));

ToDo