Package Data | |
---|---|
Maintainer Username: | AubinLrx |
Maintainer Contact: | aubin.lorieux@gmail.com (Aubin LORIEUX) |
Package Create Date: | 2015-04-24 |
Package Last Update: | 2015-07-24 |
Language: | PHP |
License: | MIT |
Last Refreshed: | 2024-12-24 03:02:56 |
Package Statistics | |
---|---|
Total Downloads: | 16 |
Monthly Downloads: | 0 |
Daily Downloads: | 0 |
Total Stars: | 12 |
Total Watchers: | 1 |
Total Forks: | 0 |
Total Open Issues: | 0 |
Repository Pattern for Laravel 5 with Eloquent ORM
Version: 0.1.5
Handy method to filter query
Usages
<?php
use AubinLrx\LaravelRepositories\Repository;
use AubinLrx\LaravelRepositories\Traits\FilterableTrait;
class BookRepository extends Repository {
use FilterableTrait;
/**
* The fields that are filterable
* @var array
*/
protected $filterable = ['author_id', 'date_range'];
public function getAllWithFilter() {
return $this->applyFilterToQuery( $this->model->query() )->all();
}
public function filterByDateRange($query, $value) {
return $query->whereBetween('date', $value);
}
public function filterBy($field, $value) {
$this->addFilter($field, $value);
return $this;
}
}
class BookController extends Controller {
public function __construct(BookRepository $books)
{
$this->books = $books;
}
public function index(Request $request)
{
$books = $this->books->filterBy('date_range', $request->only(['str_date', 'end_date']))
->filterBy('author_id', $request->only('author_id'))
->getAllWithFilter();
return view('books.index', compact('books'));
}
}
?>
v0.1.5