Replies: 1 comment
-
|
Following the instructions here <?php
namespace App\Nova\Filters;
use Illuminate\Contracts\Database\Eloquent\Builder;
use Laravel\Nova\Filters\BooleanFilter;
use Laravel\Nova\Http\Requests\NovaRequest;
use Spatie\Tags\Tag;
class Tags extends BooleanFilter
{
/**
* Create a new filter instance.
*/
public function __construct(
protected ?string $tagType = null
) {}
/**
* Apply the filter to the given query.
*/
public function apply(NovaRequest $request, Builder $query, mixed $value): Builder
{
$selectedTagIds = collect($value)->filter()->keys();
return $query
->when(
$selectedTagIds->isNotEmpty(),
fn ($q) => $q->whereHas(
'tags',
fn ($q) => $q->where('type', $this->tagType)->whereIn('id', $selectedTagIds)
)
);
}
/**
* Get the filter's available options.
*
* @return array<string, string>
*/
public function options(NovaRequest $request): array
{
return Tag::where('type', $this->tagType)->get()->pluck('id', 'name')->toArray();
}
}If you're using tag types, for example /**
* Get the filters available for the resource.
*
* @return array
*/
public function filters(NovaRequest $request)
{
return [
new Filters\Tags('my-tag-type'),
];
} |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
It would be really cool if spatie nova tags field support the ->filterable() method in Nova 4
https://nova.laravel.com/docs/4.0/releases.html#filterable-fields
Beta Was this translation helpful? Give feedback.
All reactions