PHP code example of dkulyk / nova-dependent-filter
1. Go to this page and download the library: Download dkulyk/nova-dependent-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/ */
class CategoryFilter extends DependentFilter
{
/**
* Name of filter.
*
* @var string
*/
public $name = 'Category';
/**
* Attribute name of filter. Also it is key of filter.
*
* @var string
*/
public $attribute = 'category_id';
public function options(Request $request, array $filters = [])
{
return Category::pluck('title', 'id');
}
}
function filters(Request $request)
{
return [
CategoryFilter::make(),
];
}
function filters(Request $request)
{
return [
CategoryFilter::make(),
SubCategory::make('Subcategory', 'subcategory_id')
->withOptions(function (Request $request) {
return SubCategory::all()->map(function ($subcategory) {
return [
'value' => $subcategory->id,
'label' => $subcategory->title.
'depends' => [
'category_id' => $subcategory->category_id, //Also you can set array of values
],
];
});
}),
];
}