Filter Last updated: 2022-10-04
There is no doubt that sometimes you will need to filter the results presented in the datatable
So we have prepared this for you in a couble of steps that will not consume much of your time
Suppose you want to filter some data according to the state of the data, whether it is activated or not
We will create a new object from the properties Sort or Filtering data by state
php artisan dash:make-filter ArticleStatus
ArticleStatus Filter generated
The object will look like this:
<?php
namespace App\Dash\Filters;
use Dash\Extras\Inspector\Filter;
class ArticleStatus extends Filter {
/**
* use this optional label to set custom name or can remove
* it to automatic using label from resource
* @return string
*/
public static function label() {
return 'status'; // you can use trans
}
/**
* options method to set a options
* for filtration data in index page in resource
* you can use Model with Pluck Example: User::pluck('name','id')
* @return array
*/
public static function options() {
return [
'status' => [
'show' => 'Show', // you can use trans
'hide' => 'Hide',
'pending' => 'Pending',
],
];
}
}
We will then start adding it inside a filter method in your Articles resource
/**
* define the filters To Using in Resource (index)
* php artisan dash:make-filter FilterName
* @return array
*/
public function filters() {
return [
\App\Dash\Filters\ArticleStatus::class,
];
}
Dynamic data can also be used through a model
Example of filtering by user
/**
* options method to set a options
* for filtration data in index page in resource
* you can use Model with Pluck Example: User::pluck('name','id')
* @return array
*/
public static function options() {
return [
'user_id'=>User::pluck('name','id'), // first Column
];
}
And here we are, learning something new