PHP code example of safoorsafdar / filterable

1. Go to this page and download the library: Download safoorsafdar/filterable library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

safoorsafdar / filterable example snippets



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);
        }
    }
}



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); 
    }
    
}

$query     = Account::with(['user']);
$filters   = array_filter(array_get($request->all(), 'filter', []));
if ( ! empty($filters)) {
   $query->applyFilter($filters);   
}
$result = $query->get();