All Projects → giordanolima → eloquent-repository

giordanolima / eloquent-repository

Licence: MIT license
Repository pattern for Eloquent ORM with focus in cache.

Programming Languages

PHP
23972 projects - #3 most used programming language

Projects that are alternatives of or similar to eloquent-repository

laravel-repository
Repository pattern implementation for Laravel
Stars: ✭ 49 (+63.33%)
Mutual labels:  eloquent, repository, repository-pattern
Eloquent Relativity
Allows you to decouple your eloquent models from one another.
Stars: ✭ 112 (+273.33%)
Mutual labels:  eloquent, laravel-package
Eloquent Approval
Approval process for Laravel Eloquent models
Stars: ✭ 79 (+163.33%)
Mutual labels:  eloquent, laravel-package
receptacle
minimalistic implementation of the repository pattern
Stars: ✭ 18 (-40%)
Mutual labels:  repository, repository-pattern
Simple Cache
An easy to use Caching trait for Laravel's Eloquent Models.
Stars: ✭ 19 (-36.67%)
Mutual labels:  eloquent, laravel-package
Eloquent Ldap
A Laravel 5.1 package that first tries to log the user against the internal database if that fails, it tries against the configured LDAP/AD server.
Stars: ✭ 19 (-36.67%)
Mutual labels:  eloquent, laravel-package
Blogetc
Easily add a full Laravel blog (with built in admin panel and public views) to your laravel project with this simple package.
Stars: ✭ 198 (+560%)
Mutual labels:  eloquent, laravel-package
query-filter
Define filters for your Eloquent models based on your request
Stars: ✭ 20 (-33.33%)
Mutual labels:  eloquent, laravel-package
laravel-repositories
DEPRECATED – A neat repository Pattern implementation for Laravel.
Stars: ✭ 18 (-40%)
Mutual labels:  laravel-package, repository-pattern
laravel-repository-pattern
Files autogenerator for repositorry pattern
Stars: ✭ 46 (+53.33%)
Mutual labels:  repository, repository-pattern
laravel-sybase
Connection and Laravel Eloquent driver for Sybase
Stars: ✭ 29 (-3.33%)
Mutual labels:  eloquent, laravel-package
Laravel Repositories
[ABANDONED] Rinvex Repository is a simple, intuitive, and smart implementation of Active Repository with extremely flexible & granular caching system for Laravel, used to abstract the data layer, making applications more flexible to maintain.
Stars: ✭ 664 (+2113.33%)
Mutual labels:  eloquent, repository
Laravel-Auto-Hard-Deleter
Laravel and Lumen Auto Hard Deleter
Stars: ✭ 34 (+13.33%)
Mutual labels:  eloquent, laravel-package
Laravel Schedulable
Schedule and unschedule eloquent models elegantly without cron jobs
Stars: ✭ 78 (+160%)
Mutual labels:  eloquent, laravel-package
LaraPersonate
Login as a different user quickly
Stars: ✭ 121 (+303.33%)
Mutual labels:  eloquent, laravel-package
Laravel Deletable
👾 Gracefully restrict deletion of Laravel Eloquent models
Stars: ✭ 137 (+356.67%)
Mutual labels:  eloquent, laravel-package
Model
Ruby persistence framework with entities and repositories
Stars: ✭ 399 (+1230%)
Mutual labels:  repository, repository-pattern
Onion Architecture Asp.net Core
WhiteApp API solution template which is built on Onion Architecture with all essential feature using .NET 5!
Stars: ✭ 196 (+553.33%)
Mutual labels:  repository, repository-pattern
Hexagonal-architecture-ASP.NET-Core
App generator API solution template which is built on Hexagnonal Architecture with all essential feature using .NET Core
Stars: ✭ 57 (+90%)
Mutual labels:  repository, repository-pattern
baserepo
Base repository
Stars: ✭ 71 (+136.67%)
Mutual labels:  repository, repository-pattern

Eloquent Repository

Latest Stable Version Total Downloads License StyleCI

Documentação em português.

Package to assist the implementation of the Repository Pattern using Eloquent ORM. The main feature is the flexibility and ease of use and a powerful driver for query caching.

Installation

Installing via Composer

composer require giordanolima/eloquent-repository

To configure the package options, declare the Service Provider in the config / app.php file.

'providers' => [
    ...
    GiordanoLima\EloquentRepository\RepositoryServiceProvider::class,
],

If you are using version 5.5 or higher of Laravel, the Service Provider is automatically recognized by Package Discover.

To publish the configuration file:

php artisan vendor:publish

Usage

To get started you need to create your repository class and extend the BaseRepository available in the package. You also have to set the Model that will be used to perform the queries. Example:

namespace App\Repositories;
use GiordanoLima\EloquentRepository\BaseRepository;
class UserRepository extends BaseRepository
{
    protected function model() {
        return \App\User::class;
    }
}

Now it is possible to perform queries in the same way as it is used in Elquent.

namespace App\Repositories;
use GiordanoLima\EloquentRepository\BaseRepository;
class UserRepository extends BaseRepository
{
    protected function model() {
        return \App\User::class;
    }
    
    public function getAllUser(){
        return $this->all();
    }
    
    public function getByName($name) {
        return $this->where("name", $name)->get();
    }
    
    // You can create methods with partial queries
    public function filterByProfile($profile) {
        return $this->where("profile", $profile);
    }
    
    // Them you can use the partial queries into your repositories
    public function getAdmins() {
        return $this->filterByProfile("admin")->get();
    }
    public function getEditors() {
        return $this->filterByProfile("editor")->get();
    }
    
    // You can also use Eager Loading in queries
    public function getWithPosts() {
        return $this->with("posts")->get();
    }
}

To use the class, just inject them into the controllers.

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;
use App\Repositories\UserRepository;
class UserController extends Controller
{
    protected function index(UserRepository $repository) {
        return $repository->getAdmins();
    }
}

The injection can also be done in the constructor to use the repository in all methods.

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;
use App\Repositories\UserRepository;
class UserController extends Controller
{
    private $repository;
	public function __construct()(UserRepository $repository) {
        $this->repository = $repository;
    }
    
    public function index() {
        return $this->repository->getAllUsers();
    }
    
}

The Eloquent/QueryBuilder methods are encapsulated as protected and are available just into the repository class. Declare your own public data access methods within the repository to access them through the controller.

Paginate

As default value, the paginate method uses, 15 records per page. This default value can be set in the configuration file to be used in all repositories. If necessary, you can change the default value for a single repository overwriting the perPage property with the desired value.

namespace App\Repositories;
use GiordanoLima\EloquentRepository\BaseRepository;
class UserRepository extends BaseRepository
{
    protected $perPage = 10;
    protected function model() {
        return \App\User::class;
    }
}

OrderBy

You can declare a field and a default direction to be used for all queries in a particular repository. You can still choose other fields to sort or just skip the sorting methods.

namespace App\Repositories;
use GiordanoLima\EloquentRepository\BaseRepository;
class UserRepository extends BaseRepository
{
    protected $orderBy = "created_at";
    protected $orderByDirection = "DESC";
	protected function model() {
        return \App\User::class;
    }
    
    public function getAllUser(){
        // This query will use the default ordering of the repository.
        return $this->all();
    }
    
    public function getByName($name) {
        // In this query only the declared sort will be used.
        return $this->orderBy("name")->where("name", $name)->get();
    }
    
    // É possível criar métodos com consultas parciais
    public function getWithoutOrder() {
        // In this query, no sort will be used.
        return $this->skipOrderBy()->all();
    }
    
}

GlobalScope

You can set a scope to use for all queries used in the repository. If necessary, you can also ignore this global scope.

namespace App\Repositories;
use GiordanoLima\EloquentRepository\BaseRepository;
class AdminRepository extends BaseRepository
{
    protected function model() {
        return \App\User::class;
    }
    protected function globalScope() {
        return $this->where('is_admin', true);
    }
    
    public function getAdmins() {
        // In this query the declared global scope will be included.
        return $this->all();
    }
    
    public function getAll() {
        // In this query the declared global scope will not be included.
        return $this->skipGlobalScope()->all();
    }
}

Cache

The package comes with a powerful cache driver. The idea is that once the query is done, it will be cached. After the cache is done, it is possible to reduce the number of accesses to the database to zero. All caching is performed using the cache driver configured for the application. To use the driver, just use the trait that implements it.

namespace App\Repositories;
use GiordanoLima\EloquentRepository\BaseRepository;
use GiordanoLima\EloquentRepository\CacheableRepository;
class UserRepository extends BaseRepository
{
    use CacheableRepository;
    protected function model() {
        return \App\User::class;
    }
}

Usage remains the same, with all cache management logic done automatically.

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;
use App\Repositories\UserRepository;
class UserController extends Controller
{
    private $repository;
	public function __construct()(UserRepository $repository) {
        $this->repository = $repository;
    }
    
    public function index() {
        $users = $this->repository->getAllUsers();
        // if you call the same query later (even in other requests)
        // the query is already cached and you do not need to access the database again.
        $users = $this->repository->getAllUsers(); 
    }
    
}

Everytime the database data is changed, the cache is automatically cleaned to avoid outdated data. However, if it is necessary to clear the cache, it can be performed through the clearCache() method. You can also force access to the database and avoid cached data by using the skipCache() method.

namespace App\Repositories;
use GiordanoLima\EloquentRepository\BaseRepository;
use GiordanoLima\EloquentRepository\CacheableRepository;
class UserRepository extends BaseRepository
{
    use CacheableRepository;
	protected function model() {
        return \App\User::class;
    }
    
    public function createUser($data) {
        // After inserting the data, the cache
        // is cleaned automatically.
        return $this->create($data);
    }
    
    public function addRules($user, $rules) {
        $user->rules()->attach($rules);
        $this->clearCache(); // Forcing cache cleaning.
    }
    
    public function getWithoutCache() {
        $this->skipCache()->all(); // Finding the data without using the cache.
    }
}
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].