2017 © Pedro Peláez
 

library laravel-repository

The library for build laravel repository

image

fomvasss/laravel-repository

The library for build laravel repository

  • Thursday, June 14, 2018
  • by fomvasss
  • Repository
  • 1 Watchers
  • 0 Stars
  • 78 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 6 Versions
  • 18 % Grown

The README.md

Laravel repository

Latest Stable Version Total Downloads Latest Unstable Version License, (*1)

Base class and methods for build repository pattern in Laravel and cache queries, (*2)

Installation

Run:, (*3)

    composer require fomvasss/laravel-repository

Publish config:, (*4)

php artisan vendor:publish --provider="Fomvasss\Repository\Providers\RepositoryServiceProvider" --tag="repository-config"

All base methods repository

Usage

Make own repository

Extend your repository class the next BaseRepository class, (*5)

<?php

namespace App\Repositories;

use App\Models\Article;
use Fomvasss\Repository\Eloquent\BaseRepository;

class ArticleRepository extends BaseRepository
{
    public function model()
    {
        return Article::class;
    }
    //...
}

Use repository methods

<?php
namespace App\Http\Controllers;

use App\Repositories\ArticleRepository;

class ArticleController extends BaseController {

    protected $repository;

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

    public function index() {
        $articles = $this->repository
            ->scopes(['byStatus', 1], ['sortable', ['id'=>'desc']], 'searchable')
            ->with('user')
            ->where('created_at', \Carbon\Carbon::yesterday(), '>')
            ->orderBy('created_at')
            ->get();

        //....
    }

        public function show() {
            $article = $this->repository
                ->scope('byStatus', 1)
                ->with(['user', 'categories'])
                ->where('created_at', \Carbon\Carbon::today(), '<')
                ->orderBy('created_at')
                ->firstOrFail();

            //....
        }
    //....
}

Make custom method in own repository

! Custom method do not use repository cache!, (*6)

    public function myCustomMethodByType($attributes)
    {
        $this->applyExtras();
        $models = $this->query;

        if (!empty($attributes['type'])) {
            $models = $this->query->where('type', $attributes['type']);
        }

        $this->unsetClauses();
        return $models;
    }

Events repository

Repository entity have next events: - RepositoryEntityCreated - RepositoryEntityUpdated - RepositoryEntityDeleted, (*7)

For example, you can add in your EventServiceProvider next:, (*8)

protected $listen = [
    \Fomvasss\Repository\Events\RepositoryEntityCreated::class => [
        \App\Listeners\CreatedNewModelInRepoListener::class
    ]
];

And use next method in method handle (in your listener CreatedNewModeInRepoListener):, (*9)

public function handle(RepositoryEntityCreated $event)
{
    dd($event->getAction());
    dd($event->getModel());
    dd($event->getRepository());
}

Usage repository cache

All cache methods see in Interface CacheableInterface, (*10)

Example repository with cache:, (*11)

<?php

namespace App\Repositories;

use App\Models\Article;
use Fomvasss\Repository\Contracts\CacheableInterface;
use Fomvasss\Repository\Eloquent\BaseRepository;
use Fomvasss\Repository\Traits\CacheableRepository;

class ArticleRepository extends BaseRepository implements CacheableInterface
{
    use CacheableRepository;

    protected $cacheTime = 60;

    protected $cacheTimeForMethod = [
        'all' => 10,
        'get' => 10,
        'paginate' => 10,
        'find' => 1,
    ];

    protected $cacheOnly = ['all', 'get', 'find'];

    public function model()
    {
        return Article::class;
    }
}

Middleware for off cache

Example usage middleware in routes:, (*12)

Add to App\Http\Kernel.php, (*13)

protected $routeMiddleware = [
    //...
    'rpc-off' => \Fomvasss\Repository\Http\Middleware\RepositoryCacheOff::class,
];

and use in your routes:, (*14)

Route::get('article', ArticleController@index)->middleware(['rpc-off']);

Change log

Please see CHANGELOG for more information what has changed recently., (*15)

License

The MIT License (MIT). Please see License File for more information., (*16)

The Versions

14/06 2018

dev-master

9999999-dev https://github.com/fomvasss/laravel-repository

The library for build laravel repository

  Sources   Download

MIT

The Requires

 

by Vasyl Fomin

laravel repository cache eloquent pattern-repository

14/06 2018

1.2.1

1.2.1.0 https://github.com/fomvasss/laravel-repository

The library for build laravel repository

  Sources   Download

MIT

The Requires

 

by Vasyl Fomin

laravel repository cache eloquent pattern-repository

13/06 2018

1.2.0

1.2.0.0 https://github.com/fomvasss/laravel-repository

The library for build laravel repository

  Sources   Download

MIT

The Requires

 

by Vasyl Fomin

laravel repository cache eloquent pattern-repository

25/02 2018

1.1.1

1.1.1.0 https://github.com/fomvasss/laravel-repository

The library for build laravel repository

  Sources   Download

MIT

The Requires

 

by Vasyl Fomin

laravel repository cache eloquent pattern-repository

15/01 2018

1.1.0

1.1.0.0 https://github.com/fomvasss/laravel-repository

The library for build laravel repository

  Sources   Download

MIT

The Requires

 

by Vasyl Fomin

laravel repository cache eloquent pattern-repository

12/01 2018

1.0.0

1.0.0.0 https://github.com/fomvasss/laravel-repository

The library for build laravel repository

  Sources   Download

MIT

The Requires

 

by Vasyl Fomin

laravel repository