1. Go to this page and download the library: Download baraadark/laravel-filter 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/ */
baraadark / laravel-filter example snippets
bash
php artisan vendor:publish --tag=config
php
use BaraaDark\LaravelFilter\Traits\Filterable;
class YourModel extends Model
{
use Filterable;
public function filtersKeys(): array
{
return [
// 'filter-key' => FilterClass::class
];
}
}
php
namespace App\Http\Filters\ModelName;
use BaraaDark\LaravelFilter\Filter;
class FilterClass extends Filter
{
/**
* Get the validation rules that apply to the filter request.
*
* @return array
*/
public static function rules(): array
{
return [];
}
/**
* Apply filter query on the related model.
*
* @param \Illuminate\Database\Eloquent\Builder &$query
*/
public function apply(&$query)
{
return $query;
}
}
php
use BaraaDark\LaravelFilter\Filter;
class ProductPriceRangeFilter extends Filter
{
/**
* Get the validation rules that apply to the filter request.
*
* @return array
*/
public static function rules(): array
{
return [
'min' => [' ->where('price', '<=', $this->max);
}
}
php
use BaraaDark\LaravelFilter\Traits\Filterable;
use App\Http\Filters\Product\ProductPriceRangeFilter;
class Product extends Model
{
use HasFactory, Filterable;
protected $guarded = [];
public function filtersKeys(): array
{
return [
'price-range' => ProductPriceRangeFilter::class
];
}
}
php
// config/laravel-filter.php
return [
'apply_global_scope' => false, // Set to true to enable global scope
];
php
use App\Models\SubjectCategory;
public function index(): LengthAwarePaginator
{
return SubjectCategory::applyFilter()->paginate();
}
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.