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.
- 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
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'];
}
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';
}
}
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 );
}
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 )
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
Array of relationship names. Deeply nested data can be retrieved using the DOT notation.
array(
'with' => array(
'bars'
)
);
Key/value pair of column name and direction.
array(
'order_by' => array(
'Foo' => 'DESC'
);
All keys must contain their associated value.
array(
'like' => array(
'Foo' => array(
'Bar'
),
'Bar' => array(
'Foo'
)
);
Any of the keys contain any their associated value.
array(
'or_like' => array(
'Foo' => array(
'Bar'
),
'Bar' => array(
'Foo'
)
);
Non of the keys contain their associated value.
array(
'not_like' => array(
'Foo' => array(
'Bar'
),
'Bar' => array(
'Foo'
)
);
All keys must match their associated value.
array(
'equal' => array(
'Foo' => array(
'Bar'
)
);
Any of the keys match the value
array(
'or_equal' => array(
'Foo' => array(
'Foo1'
),
'Bar' => array(
'Bar1'
)
);
All keys must contain a value greater than or equal too their associated value.
array(
'gte' => array(
'Foo' => array(
1
)
);
All keys must contain a value greater than their associated value.
array(
'gt' => array(
'Foo' => array(
1
)
);
All keys must contain a value less than or equal too their associated value.
array(
'lte' => array(
'Foo' => array(
99
)
);
All keys must contain a value less than their associated value.
array(
'lt' => array(
'Foo' => array(
99
)
);
The key contains any of the values listed in their associated value(s)
array(
'in_array' => array(
'Foo' => array(
'Bar1',
'Bar2',
'Bar3'
)
);
The key contains any of the values listed in their associated value(s)
array(
'between' => array(
'Foo' => array(
1,
5
)
);
The key contains a value greater than the first value and less than the second value.
array(
'between' => array(
'Foo' => array(
1,
5
)
);
All unit tests are run against an SQLite database.