Eloquent Repository implementation

Overview

Eloquent Repository

PHP7 Tested Build Status Scrutinizer Code Quality SensioLabsInsight Latest Stable Version Total Downloads License Donate

Eloquent Repository using nilportugues/repository as foundation.

Installation

Use Composer to install the package:

$ composer require nilportugues/eloquent-repository

Why? Drivers and Multiple Implementations!

Using this implementation you can switch it out to test your code without setting up databases.

Doesn't sound handy? Let's think of yet another use case you'll love using this. Functional tests and Unitary tests.

No database connection will be needed, nor fakes. Using an InMemoryRepository or FileSystemRepository implementation will make those a breeze to code. And once the tests finish, all data may be destroyed with no worries at all.

Available drivers:

Also, if you feel like changing the repository implementation, no logic changes would be needed, as there are a set of drivers for you to use out of the box:

Usage

To set up Eloquent you don't need Laravel or Lumen frameworks at all. This is how you use Eloquent in any project.

<?php
use Illuminate\Database\Capsule\Manager as Capsule;

$capsule = new Capsule();
$capsule->addConnection(
    [
        'driver' => 'sqlite', 
        'database' => __DIR__.'/database.db',
        'prefix' => ''
    ], 
    'default' //connection name.
);
$capsule->bootEloquent();
$capsule->setAsGlobal();

Now that Eloquent is running, we can use the Repository.

One Repository for One Eloquent Model

A well defined repository returns one kind of objects that belong to one Business model.

<?php
use NilPortugues\Foundation\Infrastructure\Model\Repository\Eloquent\EloquentRepository;

class UserRepository extends EloquentRepository 
{
    /**
     * {@inheritdoc}
     */
    protected function modelClassName()
    {
        return User::class;
    }
}

To be faithful to the repository pattern, using Eloquent Models internally is OK, but Business objects should be returned.

Therefore, you should translate Eloquent to Business representations and the other way round. This is represented by $userAdapter in the example below.

The fully implementation should be along the lines:

<?php
use NilPortugues\Foundation\Infrastructure\Model\Repository\Eloquent\EloquentRepository;

class UserRepository extends EloquentRepository 
{
    protected $userAdapter;
    
    /**
     * @param $userAdapter
     */
    public function __construct($userAdapter)
    {
        $this->userAdapter = $userAdapter; 
    }
    
    /**
     * {@inheritdoc}
     */
    protected function modelClassName()
    {
        return User::class;
    }
    
    /**
     * {@inheritdoc}
     */    
    public function find(Identity $id, Fields $fields = null)
    {
        $eloquentModel = parent::find($id, $fields);   
        
        return $this->userAdapter->fromEloquent($eloquentModel);
    }
    
    /**
     * {@inheritdoc}
     */    
    public function findBy(Filter $filter = null, Sort $sort = null, Fields $fields = null)
    {
        $eloquentModelArray = parent::findBy($filter, $sort, $fields);   
        
        return $this->fromEloquentArray($eloquentModelArray);
    }       
    
    /**
     * {@inheritdoc}
     */
    public function findAll(Pageable $pageable = null)
    {
        $page = parent::findAll($pageable);
        
        return new Page(
            $this->fromEloquentArray($page->content()),
            $page->totalElements(),
            $page->pageNumber(),
            $page->totalPages(),
            $page->sortings(),
            $page->filters(),
            $page->fields()
        );
    } 

   /**
    * @param array $eloquentModelArray
    * @return array
    */
   protected function fromEloquentArray(array $eloquentModelArray)
   {
        $results = [];
        foreach ($eloquentModelArray as $eloquentModel) {
            //This is required to handle findAll returning array, not objects.
            $eloquentModel = (object) $eloquentModel;
            
            $results[] = $this->userAdapter->fromEloquent($eloquentModel);
        }
        
        return $results;
   } 
}

A sample implementation can be found in the /example directory.

One EloquentRepository for All Eloquent Models

While this is not the recommended way, as a repository should only return one kind of Business objects, this works well with Laravel projects.

While the amount of core is less than the previous example, bare in mind that your code will be coupled with Eloquent.

<?php
use NilPortugues\Foundation\Infrastructure\Model\Repository\Eloquent\EloquentRepository as Repository;

class EloquentRepository extends Repository
{
    /**
     * @var string
     */
    protected $modelClass;
    
    /**
     * @param string $modelClass
     */
    public function __construct($modelClass)
    {
        $this->modelClass = (string) $modelClass;
    }
    
    /**
     * {@inheritdoc}
     */
    protected function modelClassName()
    {
        return $this->modelClass;
    }
}

Filtering data

Filtering is as simple as using the Filter object. For instance, lets retrieve how many users are named Ken.

<?php
use NilPortugues\Foundation\Domain\Model\Repository\Filter;

$repository = new UserRepository();

$filter = new Filter();
$filter->must()->contain('name', 'Ken');

echo $repository->count($filter);

Notice how the key name matches the database column name in the users table.

Available options

Filter allow you to use must(), mustNot() and should() methods to set up a fine-grained search. These provide a fluent interface with the following methods available:

  • public function notEmpty($filterName)
  • public function hasEmpty($filterName)
  • public function startsWith($filterName, $value)
  • public function endsWith($filterName, $value)
  • public function equal($filterName, $value)
  • public function notEqual($filterName, $value)
  • public function includeGroup($filterName, array $value)
  • public function notIncludeGroup($filterName, array $value)
  • public function range($filterName, $firstValue, $secondValue)
  • public function notRange($filterName, $firstValue, $secondValue)
  • public function notContain($filterName, $value)
  • public function contain($filterName, $value)
  • public function beGreaterThanOrEqual($filterName, $value)
  • public function beGreaterThan($filterName, $value)
  • public function beLessThanOrEqual($filterName, $value)
  • public function beLessThan($filterName, $value)

Sorting data

Sorting is straight forward. Create an instance of Sort and pass in the column names and ordering.

<?php
use NilPortugues\Foundation\Domain\Model\Repository\Sort;

$repository = new UserRepository();

$filter = null; //all records
$sort = new Sort(['name', 'id'], new Order('ASC', 'DESC'));
$fields = null; //all columns

$results = $repository->findBy($filter, $sort, $fields);

Fields data

Create a Fields object to fetch only selected columns. If no Fields object is passed, all columns are selected by default.

<?php
use NilPortugues\Foundation\Domain\Model\Repository\Contracts\Fields;

$repository = new UserRepository();

$filter = null; //all records
$sort = null; //existing order
$fields = new Fields(['name', 'id']);

$results = $repository->findBy($filter, $sort, $fields);

Fetching data

Repository allows you to fetch data from the database by using the following methods:

  • public function findAll(Pageable $pageable = null)
  • public function find(Identity $id, Fields $fields = null)
  • public function findBy(Filter $filter = null, Sort $sort = null, Fields $fields = null)

Quality

To run the PHPUnit tests at the command line, go to the tests directory and issue phpunit.

This library attempts to comply with PSR-1, PSR-2, PSR-4.

If you notice compliance oversights, please send a patch via Pull Request.

Contribute

Contributions to the package are always welcome!

Support

Get in touch with me using one of the following means:

Authors

License

The code base is licensed under the MIT license.

You might also like...
A drop-in Doctrine ORM 2 implementation for Laravel 5+ and Lumen
A drop-in Doctrine ORM 2 implementation for Laravel 5+ and Lumen

Laravel Doctrine ORM A drop-in Doctrine ORM 2 implementation for Laravel 5+ $scientist = new Scientist( 'Albert', 'Einstein' ); $scientist-a

🔌 A Doctrine DBAL Driver implementation on top of Swoole Coroutine PostgreSQL extension

Swoole Coroutine PostgreSQL Doctrine DBAL Driver A Doctrine\DBAL\Driver implementation on top of Swoole\Coroutine\PostgreSQL. Getting started Install

A minimalistic implementation of asynchronous SQL for PHP.

libSQL A minimalistic implementation of asynchronous SQL for PHP. Installation via DEVirion Install the DEVirion plugin and start your server. This wi

Async Redis client implementation, built on top of ReactPHP.

clue/reactphp-redis Async Redis client implementation, built on top of ReactPHP. Redis is an open source, advanced, in-memory key-value database. It o

a distributed-redis-lock implementation for hyperf2.*

hyperf-redis-lock English | 中文 an easy redis-based distributed-lock implementation for hyperf 2.*。 This extension features distributed-lock includes b

Eloquent Repository implementation

Eloquent Repository Eloquent Repository using nilportugues/repository as foundation. Installation Use Composer to install the package: $ composer requ

Eloquent MongoDB Repository Implementation

Eloquent MongoDB Repository Eloquent MongoDB Repository using nilportugues/repository as foundation, using jenssegers/mongodb. Installation Use Compos

Fast and simple implementation of a REST API based on the Laravel Framework, Repository Pattern, Eloquent Resources, Translatability, and Swagger.

Laravel Headless What about? This allows a fast and simple implementation of a REST API based on the Laravel Framework, Repository Pattern, Eloquent R

Auto-generated Interface and Repository file via Repository pattern in Laravel

Auto-generated Repository Pattern in Laravel A repository is a separation between a domain and a persistent layer. The repository provides a collectio

Baum is an implementation of the Nested Set pattern for Laravel's Eloquent ORM.

Baum Baum is an implementation of the Nested Set pattern for Laravel 5's Eloquent ORM. For Laravel 4.2.x compatibility, check the 1.0.x branch branch

Simplified Repository pattern implementation in Laravel

Laravository - Repository Pattern for Laravel Simplified Repository pattern implementation in Laravel. Requirement Laravel 8.x Installation Execute th

Redis repository implementation for Laravel Queue Batch

Laravel RedisBatchRepository Replaces default Illuminate\Bus\DatabaseBatchRepository with implementation based on Redis. Requirements: php 8 laravel 8

Repository Pattern implementation for Laravel

This is a Simple Repository Pattern implementation for Laravel Projects and an easily way to build Eloquent queries from API requests.

Composer repository implementation for ZIPs.

Release Belt — Composer repo for ZIPs Release Belt is a Composer repository, which serves to quickly integrate third–party non–Composer releases into

This repository demonstrates exemplary implementation of chat using HTTP and Websocket servers in PHP using Kraken Framework components.
This repository demonstrates exemplary implementation of chat using HTTP and Websocket servers in PHP using Kraken Framework components.

This repository demonstrates exemplary implementation of chat using HTTP and Websocket servers in PHP using Kraken Framework components.

A PHP implementation of the GraphQL specification based on the JavaScript reference implementation

GraphQL This is a PHP implementation of the GraphQL specification based on the JavaScript reference implementation. Related projects DateTime scalar R

⚡️ Models like Eloquent for Elasticsearch.

Elasticsearch Eloquent 2.x This package allows you to interact with Elasticsearch as you interact with Eloquent models in Laravel. Requirements PHP =

Eloquent roles and abilities.
Eloquent roles and abilities.

Bouncer Bouncer is an elegant, framework-agnostic approach to managing roles and abilities for any app using Eloquent models. Table of Contents Click

An Eloquent Way To Filter Laravel Models And Their Relationships

Eloquent Filter An Eloquent way to filter Eloquent Models and their relationships Introduction Lets say we want to return a list of users filtered by

Comments
  • [Insight] Object parameters should be type hinted - in example/Service/UserAdapter.php, line 16

    [Insight] Object parameters should be type hinted - in example/Service/UserAdapter.php, line 16

    in example/Service/UserAdapter.php, line 16

    The parameter model, which is an object, should be typehinted.

        /**
         * @param $model
         *
         * @return User
         */
        public function fromEloquent($model)
        {
            return new User(
                new UserId($model->id),
                $model->name,
                new DateTimeImmutable($model->created_at)
    

    Posted from SensioLabsInsight

    opened by esterm 2
  • Missing NOT_GROUP filter

    Missing NOT_GROUP filter

    https://github.com/nilportugues/php-eloquent-repository/blob/master/src/Infrastructure/Model/Repository/Eloquent/EloquentFilter.php#L92

    Was implemented in InMemoryRepository but not here

    opened by nilportugues 1
  • Filters where equal

    Filters where equal

    Buenas, he visto el repositorio y estoy haciendo unas pruebas, la duda que tengo es para hacer la siguiente consulta: select count(id) from table where id = value and other_field = 'value2';

    He seguido la estructura del filter, que sería así: $filters = new Filter(); $filters->must()->equal('id', value); $filters->must()->equal('other_field', 'value2');

    total = parent::count($filters);

    mi duda está en que el processCondition peta en querer comprobar el valor si es array if (count($value[0]) > 1) { $where = static::filterRanges.......... } else { $where = static::filterGroup.......... } Cuando realmente no quiero que entre en rango, ni en grupo, pero los filtros, siempre los trata como array.

    Cual puede ser la solución.

    Gracias

    opened by noelyriosquantion 0
  • Questions

    Questions

    Hi, this package looks great, well done (y).

    Nested Queries

    How would you approach a nested query? say we have a user with posts and I'd like to get the user with the latest 3 posts titles.

    Validation

    Do you just use laravel's validation? the problem is you'll be using the query builder when you do this and this kind of kills the abstraction layer a repository is supposed to provide.

    Sorry if these questions are kind of off topic, they're just one of the reasons the repository pattern never seemed to work for me with eloquent, you always find yourself rewriting every cool feature of Laravel!

    opened by yazfield 0
Releases(2.2.2)
Owner
Nil Portugués Calderó
Nil Portugués Calderó
Baum is an implementation of the Nested Set pattern for Laravel's Eloquent ORM.

Baum Baum is an implementation of the Nested Set pattern for Laravel 5's Eloquent ORM. For Laravel 4.2.x compatibility, check the 1.0.x branch branch

Estanislau Trepat 2.2k Jan 3, 2023
⚡️ Models like Eloquent for Elasticsearch.

Elasticsearch Eloquent 2.x This package allows you to interact with Elasticsearch as you interact with Eloquent models in Laravel. Requirements PHP >=

Sergey Sorokin 111 Dec 19, 2022
A MongoDB based Eloquent model and Query builder for Laravel (Moloquent)

Laravel MongoDB This package adds functionalities to the Eloquent model and Query builder for MongoDB, using the original Laravel API. This library ex

Jens Segers 6.3k Jan 5, 2023
Extensions for the Eloquent ORM

Sofa/Eloquence Easy and flexible extensions for the Eloquent ORM. Currently available extensions: Searchable query - crazy-simple fulltext search thro

Jarek Tkaczyk 1.1k Dec 20, 2022
Easily exclude model entities from eloquent queries

Laravel Excludable Easily exclude model entities from eloquent queries. This package allows you to define a subset of model entities who should be exc

H-FARM 49 Jan 4, 2023
Simple Enum cast for Eloquent ORM using myclabs/php-enum.

Enum cast for Eloquent Simple Enum cast for Eloquent ORM using myclabs/php-enum. Requirements PHP 7.3 or higher Laravel 8.0 or higher Installation You

Orkhan Ahmadov 5 Apr 21, 2022
Eloquent Filter is a package for filter data of models by the query strings. Easy to use and fully dynamic.

Eloquent Filter Eloquent Filter adds custom filters to your Eloquent Models in Laravel. It's easy to use and fully dynamic. Table of Content Introduct

Mehdi Fathi 327 Dec 28, 2022
Satis composer repository manager with a Web UI

Satisfy Satis Composer repository manager with a simple web UI. Introduction Satisfy provides: a Web UI: A CRUD to manage your satis configuration fil

Ludovic Fleury 470 Dec 28, 2022
A data mapper implementation for your persistence model in PHP.

Atlas.Orm Atlas is a data mapper implementation for persistence models (not domain models). As such, Atlas uses the term "record" to indicate that its

null 427 Dec 30, 2022
Adjacency List’ed Closure Table database design pattern implementation for the Laravel framework.

ClosureTable This is a database manipulation package for the Laravel 5.4+ framework. You may want to use it when you need to store and operate hierarc

Yan Ivanov 441 Dec 11, 2022