Skip to content

jacksoncharles/laravel-searchable-endpoints

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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.

This package has been tested against illuminate ^5.2 as used by Laravel/Lumen 5.3.

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

Implementation

Model

Start by creating an Eloquent model

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.

    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.

...

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.

    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.

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

with

Array of relationship names. Deeply nested data can be retrieved using the DOT notation.

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

order_by

Key/value pair of column name and direction.

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

like

All keys must contain their associated value.

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

or_like

Any of the keys contain any their associated value.

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

not_like

Non of the keys contain their associated value.

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

equal

All keys must match their associated value.

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

or_equal

Any of the keys match the value

    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.

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

gt

All keys must contain a value greater than their associated value.

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

lte

All keys must contain a value less than or equal too their associated value.

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

lt

All keys must contain a value less than their associated value.

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

in_array

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

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

in

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

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

between

The key contains a value greater than the first value and less than the second value.

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

Tests

All unit tests are run against an SQLite database.

Contributors

Charles Jackson

About

Easily Searchable Repositories for Illuminate/database

Resources

Stars

Watchers

Forks

Packages

No packages published

Languages