Laravel record filter provides an easy and intuitive way to filter and sort records in your Laravel applications. It mimics the familiar syntax of the Laravel Paginator, making it simple to implement complex filtering logic.
This package requires your app to include tailwind and alpinejs in your views
You can install the package via composer:
composer require musta20/laravel-records-filter
make sure to rebuild tailwind assets if you are not running vite dev
yarn vite build
In your model use the trait HasFilter:
use Musta20\LaravelRecordsFilter\HasFilter;
class Post extends Model
{
use HasFactory, HasFilter;
The following example show how to implement sorting
public function sortFilterOptions()
{
return [
[
"lable" => "oldest", // label name to display in the filtering form
"type" => 'ASC', // sorting type
"filed" => "created_at" // talbel filed name
],
[
"lable" => "newest",
"type" => 'DESC',
"filed" => "created_at"
],
[
"lable" => "high price",
"type" => 'DESC',
"filed" => "price"
],
[
"lable" => " lowest price",
"type" => 'ASC',
"filed" => "price"
],
];
}
You can filter record based on relation to other table:
public function relationsFilterOptions()
{
return [
[
'label' => 'auther', // label name to display in the filtering form
'label_filed' => 'name', // filed name in the relation tabel
'id' => 'user_id', // label name in the relation tabel
'model' => 'App\Models\User',
],
[
'label' => 'category',
'label_filed' => 'name',
'id' => 'category_id',
'model' => 'App\Models\category',
]
];
}
You can search in records by defining the table name in search function:
public function searchFields()
{
return [
'title',
'body'
];
}
You can define filtering term like the following :
public function filterOptions()
{
return [
[
'label' => 'reviewing', // label name to display in the filtering form
'type' => 'checkbox', // checkbox | select | radio group | date | between
'filed' => 'article_type', // filed name in the tabel
'operation' => '=', // opertaion type
'options' => [ // the values you want to filter based on
'reviewed' => 1, // the first key will be used as label
'not reviewed' => 0
],
],
[
'label' => 'publish status',
'type' => 'select', //checkbox | select | radio group | date | between
'filed' => 'is_published',
'options' => [
'drafted' => 0,
'publish' => 1
]
],
[
'label' => 'font size',
'type' => 'radio group', //checkbox | select | radio group | date | between
'filed' => 'font_type',
'options' => [
'small' => 10,
'big' => 20
]
],
[
'label' => 'issue date',
'type' => 'date', //checkbox | select | radio group | date | between
'filed' => 'created_at',
'operation' => '='
]
,
[
'label' => 'issue period',
'type' => 'range', //checkbox | select | radio group | date | between
'filed' => 'created_at',
'inputType' => 'date',//date/number
'operation' => 'between',
'options' => [
'from',
'to'
]
]
,
[
'label' => 'price range',
'type' => 'range', //checkbox | select | radio group | date | between
'filed' => 'price',
'inputType' => 'number',//date/number
'operation' => 'between',
'options' => [
'max',
'min'
]
]
];
}
you must use the function Filter() to retrieve the data keep in mind you have to call it the last in the query and do not call the paginate as it automatically paginated
$posts= Post::filter();
// or
$posts= Post::where('name','alie')->filter();
Finally, run in your view call the filter nav using the function.
{{ $posts->filterLinks() }}
You can use different view like nav-filter to show all filtering option in linear view
{{ $posts->filterLinks('laravelRecordsFilter::nav-filter') }}
You can also display filtering option in sidebar view by using the following function
for the side bar :
{{ $posts->filterLinks('laravelRecordsFilter::sidebar-filter') }}
for the nav :
{{ $posts->filterNav() }}
now of curse the view will not always match your style so you can publish the view and edit it the way you want
php artisan vendor:publish --tag=laravel-Records-Filter
The MIT License (MIT). Please see License File for more information.