BannerFilter.php
1.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
<?php
namespace App\ModelFilters;
use App\Support\ModelFilter;
use Arr;
class BannerFilter extends ModelFilter
{
/**
* @param string $name
* @return void
*/
public function name(string $name): void
{
$this->whereLike('name', $name);
}
/**
* @param string|array $role
* @return void
*/
public function role(string|array $role): void
{
$this->whereJsonContains('role', Arr::wrap($role));
}
/**
* @param array|int|string $scope
* @return void
*/
public function scope(array|int|string $scope): void
{
$this->whereIn('scope', Arr::wrap($scope));
}
/**
* @param string|array $permission
* @return void
*/
public function permission(string|array $permission): void
{
$permission = Arr::wrap($permission);
$count = count($permission);
if ($count <= 1) {
$this->whereJsonContains('permission', $permission);
} else {
$this->where(fn($query) => Arr::map($permission, static fn($item) => $query->orWhereJsonContains('permission', $item)));
// $this->whereJsonContains('permission')
// $this->whereRaw('JSON_EXTRACT(permission, \'$[*]\') IN (' . implode(',', array_fill(0, $count, '?')) . ')', $permission);
// $this->whereIn('permission', Arr::wrap($permission));
// $this->whereRaw('JSON_CONTAINS(JSON_ARRAY(' . implode(',', array_fill(0, $count, '?')) . '),JSON_PRETTY(permission))', $permission);
}
}
/**
* @param array|int|string $status
* @return void
*/
public function status(array|int|string $status): void
{
$this->whereIn('status', Arr::wrap($status));
}
}