Package Data | |
---|---|
Maintainer Username: | safoorsafdar |
Maintainer Contact: | safoor.safdar@gmail.com (SafoorSafdar) |
Package Create Date: | 2017-05-05 |
Package Last Update: | 2017-05-05 |
Language: | PHP |
License: | MIT |
Last Refreshed: | 2024-12-22 03:09:13 |
Package Statistics | |
---|---|
Total Downloads: | 5 |
Monthly Downloads: | 0 |
Daily Downloads: | 0 |
Total Stars: | 0 |
Total Watchers: | 2 |
Total Forks: | 0 |
Total Open Issues: | 0 |
Filterable is an package helps to filter the eloquent model based on various condition which already ship with the package.
Via Composer
$ composer require safoorsafdar/filterable
<?php
namespace App\Models\Account;
use SafoorSafdar\Filterable\Traits\FilterableTrait;
use Illuminate\Database\Eloquent\Model;
/**
* Class Account
*
* @package App\Models\Account
*/
class Account extends Model
{
use FilterableTrait
protected static $filters
= [
"name" => \App\Models\Account\Filters\AccountNameFilter::class,
];
}
Note $filterable
array contain the current table attribute name in the database and reference to filter class which will performed the query.
<?php
namespace App\Models\Account\Filters;
use Illuminate\Database\Eloquent\Builder;
use SafoorSafdar\Filterable\Filter\Filter;
class AccountNameFilter extends Filter
{
/**
* Apply a given search value to the builder instance.
*
* @param Builder $builder
* @param mixed $value
*
* @return Builder $builder
*/
public static function apply(
Builder $builder,
$value,
$condition,
$operator
) {
$operatorDecorator = self::createOperatorDecorator($condition);
if (self::isValidDecorator($operatorDecorator)) {
return app($operatorDecorator)->resolve($builder, 'name', $value,
$operator);
}
}
}
<?php
namespace App\Http\Controllers;
use App\Http\Controllers\Controller;
class AccountController extends Controller
{
public function index(){
$filters = $request->get('filter', []);
return view('account.index')
->withFilterFields(\App\Models\Account\Account::filterableAttributes())
->withFilterCondition(\Filterable::operators())
->with("filtered", $filters);
}
}
@include('Filterable::partial.filterable',['filtered'=>$filtered,'filter_fields'=>$filter_fields,'filter_condition'=>$filter_condition])
<script type="text/javascript" src="/js/filterable.js"></script>
$query = Account::with(['user']);
$filters = array_filter(array_get($request->all(), 'filter', []));
if ( ! empty($filters)) {
$query->applyFilter($filters);
}
$result = $query->get();
Please see CHANGELOG for more information on what has changed recently.
$ composer test
Please see CONTRIBUTING and CONDUCT for details.
If you discover any security related issues, please email safoor.safdar@gmail.com instead of using the issue tracker.
The MIT License (MIT). Please see License File for more information.