2017 © Pedro Peláez
 

library illuminate-searchable-repositories

Illuminate database repository package specifically designed to facilitate the easy creation of searchable API endpoints by accepting multi-dimensional arrays to build a select statement. Suitable for Laravel, Lumen or any project using the Illuminate package.

image

webconfection/illuminate-searchable-repositories

Illuminate database repository package specifically designed to facilitate the easy creation of searchable API endpoints by accepting multi-dimensional arrays to build a select statement. Suitable for Laravel, Lumen or any project using the Illuminate package.

  • Sunday, July 2, 2017
  • by jacksoncharles
  • Repository
  • 2 Watchers
  • 0 Stars
  • 5 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 4 Versions
  • 0 % Grown

The README.md

Illuminate Searchable Repositories

Implementation of the Repository Pattern for the Illuminate Database Package specifically designed to facilitate the easy creation of searchable API endpoints. In addition to 10 conventional methods there are a further 13 search criteria which can be passed to your repository as multidimensional arrays using the setParameters() solution., (*1)

This package has been tested against illuminate ^5.2 as used by Laravel/Lumen 5.3., (*2)

  1. Installation
  2. Implementation
    1. Model
    2. Repository
    3. Example Controller
  3. Methods
  4. Parameter Trait
    1. With
    2. Order By
    3. Like
    4. Or Like
    5. Not Like
    6. Equal
    7. Or Equal
    8. Greater Than or Equal
    9. Greater Than
    10. Less Than or Equal
    11. Less Than
    12. In Array
    13. Between
  5. Tests
  6. Contributors

Implementation

Model

Start by creating an Eloquent model, (*3)

namespace App;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;

class FooBar extends Model {

    use SoftDeletes;

    /**
     * @var string
     *
     * The table for the model.
     */
    protected $table = 'foo_bars';

    /**
     * @var array
     *
     * Columns that can be filled by mass assigment
     */
    protected $fillable = ['id','body'];
}

Repository

Create a repository and extend to use WebConfection\Repositories\Repository and implement the WebConfection\Repositories\Interfaces\RepositoryInterface. Then create a model() method returning a namespaced string to the model you just created., (*4)

    namespace App\Repositories;

    use WebConfection\Repositories\Repository;
    use WebConfection\Repositories\Interfaces\RepositoryInterface;

    class FooBarRepository extends Repository implements RepositoryInterface
    {
        /**
         * Specify Model class name
         *
         * @return mixed
         */
        function model()
        {
            return 'App\FooBar';
        }
    }

Example Controller ::index

In the following controller::index I have injected the repository into the __construct of my controller. It is a matter of personal preference., (*5)

...

class FooBarsController extends Controller
{
    public function __construct( FooBarInterface $fooBarRepository )
    {
       $this->repository = $fooBarRepository;
    }

    /**
     * Return a JSON encoded listing including filters
     *
     * @return array
     */
    public function index()
    {

        if( Input::has('parameters') ) $this->repository->setParameters( Input::get('parameters') );

        if( Input::has('rows') )
        {
            $data = $this->repository->paginate( Input::get('rows'), Input::get('columns'), Input::get('page'), Input::has('trash') )->toArray();
        } 
        else
        {
            $data = $this->repository->all( , Input::get('columns'), Input::has('trash') )->toArray();
        }

        return response()->json( $data, 200 );
    }

Methods

Further details can be found inside the interface., (*6)

    public function all( $columns = ['*'], $withTrash = false )

    public function paginate( $rows = 10, $columns = ['*'], $page = false, $withTrash = false )

    public function find( $id, $columns = ['*'], $withTrash = false )

    public function findBy( array $attributes, $columns = ['*'], $withTrash = false ) 

    public function first( $columns = ['*'], $withTrash = flase ) 

    public function count( $withTrash = false )

    public function create( array $data ) 

    public function update( $id, array $data )

    public function delete( $id )

    public function forceDelete( $id )

Parameter Trait

The parameter trait enables you to pass a multi-dimensional array into your repository using the setParameters() method. The parameter will then be used to build your query and the results can be retrieved using of any the conventional repository methods: all, paginate, find, findBy, first and count., (*7)

13 comparison operators are currently supported. Where the key of each array maps to a searchable item (eg: database column) in permanent storage. Further examples can be found inside the test suite, (*8)

with

Array of relationship names. Deeply nested data can be retrieved using the DOT notation., (*9)

    array(
        'with'  =>  array(
            'bars'
        )
    );

order_by

Key/value pair of column name and direction., (*10)

    array(
        'order_by'  =>  array(
            'Foo'   =>  'DESC'
    );

like

All keys must contain their associated value., (*11)

    array(
        'like'  =>  array(
            'Foo'   =>  array(
                'Bar'
            ),
            'Bar'   =>  array(
                'Foo'
            )
    );

or_like

Any of the keys contain any their associated value., (*12)

    array(
        'or_like'   =>  array(
            'Foo'   =>  array(
                'Bar'
            ),
            'Bar'   =>  array(
                'Foo'
            )
    );

not_like

Non of the keys contain their associated value., (*13)

    array(
        'not_like'   =>  array(
            'Foo'   =>  array(
                'Bar'
            ),
            'Bar'   =>  array(
                'Foo'
            )
    );

equal

All keys must match their associated value., (*14)

    array(
        'equal' =>  array(
            'Foo'   =>  array(
                'Bar'
            )
    );

or_equal

Any of the keys match the value, (*15)

    array(
        'or_equal' =>  array(
            'Foo'   =>  array(
                'Foo1'
            ),
            'Bar'   =>  array(
                'Bar1'
            )

    );

gte

All keys must contain a value greater than or equal too their associated value., (*16)

    array(
        'gte'   =>  array(
            'Foo'   =>  array(
                1
            )
    );

gt

All keys must contain a value greater than their associated value., (*17)

    array(
        'gt'    =>  array(
            'Foo'   =>  array(
                1
            )
    );

lte

All keys must contain a value less than or equal too their associated value., (*18)

    array(
        'lte'   =>  array(
            'Foo'   =>  array(
                99
            )
    );

lt

All keys must contain a value less than their associated value., (*19)

    array(
        'lt'    =>  array(
            'Foo'   =>  array(
                99
            )
    );

in_array

The key contains any of the values listed in their associated value(s), (*20)

    array(
        'in_array'    =>  array(
            'Foo'   =>  array(
                'Bar1',
                'Bar2',
                'Bar3'
            )
    );

in

The key contains any of the values listed in their associated value(s), (*21)

    array(
        'between'    =>  array(
            'Foo'   =>  array(
                1,
                5
            )
    );

between

The key contains a value greater than the first value and less than the second value., (*22)

    array(
        'between'    =>  array(
            'Foo'   =>  array(
                1,
                5
            )
    );

Tests

All unit tests are run against an SQLite database., (*23)

Contributors

Charles Jackson, (*24)

The Versions

02/07 2017

dev-master

9999999-dev

Illuminate database repository package specifically designed to facilitate the easy creation of searchable API endpoints by accepting multi-dimensional arrays to build a select statement. Suitable for Laravel, Lumen or any project using the Illuminate package.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Charles Jackson

illuminate repositories laravel

04/10 2016

1.0.2

1.0.2.0

Illuminate database repository package specifically designed to facilitate the easy creation of searchable API endpoints by accepting multi-dimensional arrays to build a select statement. Suitable for Laravel, Lumen or any project using the Illuminate package.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Charles Jackson

illuminate repositories laravel

04/10 2016

1.0.1

1.0.1.0

Illuminate database package specifically designed to facilitate the easy creation of searchable API endpoints by accepting multi-dimensional arrays to build a select statement

  Sources   Download

MIT

The Development Requires

by Charles Jackson

illuminate repositories laravel