Total Complexity | 6 |
Total Lines | 44 |
Duplicated Lines | 0 % |
Coverage | 100% |
Changes | 1 | ||
Bugs | 0 | Features | 0 |
1 | <?php |
||
10 | class BelongsToManyFilter extends BooleanFilter |
||
11 | { |
||
12 | protected string $relationName; |
||
13 | protected string $titleKeyName = 'name'; |
||
14 | |||
15 | 3 | public function __construct(string $relationName) |
|
16 | { |
||
17 | 3 | $this->relationName = $relationName; |
|
18 | 3 | $this->name = Str::ucfirst(Str::snake($this->relationName, ' ')); |
|
19 | } |
||
20 | |||
21 | 1 | public function apply(NovaRequest $request, $query, $value) |
|
22 | { |
||
23 | 1 | $selected = array_filter($value); |
|
24 | 1 | if (count($selected)) { |
|
25 | 1 | return $query->whereHas( |
|
26 | 1 | $this->relationName, |
|
27 | 1 | fn (Builder $q) => $q->whereKey(array_keys($selected)) |
|
28 | 1 | ); |
|
29 | } |
||
30 | |||
31 | 1 | return $query; |
|
32 | } |
||
33 | |||
34 | 2 | public function options(NovaRequest $request) |
|
40 | } |
||
41 | |||
42 | 3 | public function setTitleKeyName(string $titleKeyName): static |
|
43 | { |
||
44 | 3 | $this->titleKeyName = $titleKeyName; |
|
45 | |||
46 | 3 | return $this; |
|
47 | } |
||
48 | |||
49 | 2 | public function setFilterName(string $name): static |
|
54 | } |
||
55 | } |
||
56 |