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)
- Installation
-
Implementation
- Model
- Repository
- Example Controller
- Methods
-
Parameter Trait
- With
- Order By
- Like
- Or Like
- Not Like
- Equal
- Or Equal
- Greater Than or Equal
- Greater Than
- Less Than or Equal
- Less Than
- In Array
- Between
- Tests
- 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)