|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Xetaravel\Livewire\Admin\Badge; |
|
6
|
|
|
|
|
7
|
|
|
use Illuminate\Contracts\Database\Query\Builder; |
|
8
|
|
|
use Illuminate\Contracts\Pagination\LengthAwarePaginator; |
|
9
|
|
|
use Illuminate\Contracts\View\Factory; |
|
10
|
|
|
use Illuminate\Contracts\View\View; |
|
11
|
|
|
use Illuminate\Foundation\Application; |
|
12
|
|
|
use Illuminate\Foundation\Auth\Access\AuthorizesRequests; |
|
13
|
|
|
use Illuminate\Support\Facades\Gate; |
|
14
|
|
|
use Livewire\Attributes\Url; |
|
15
|
|
|
use Livewire\Component; |
|
16
|
|
|
use Livewire\WithPagination; |
|
17
|
|
|
use Masmerise\Toaster\Toastable; |
|
18
|
|
|
use Xetaravel\Livewire\Traits\WithBulkActions; |
|
19
|
|
|
use Xetaravel\Livewire\Traits\WithPerPagePagination; |
|
20
|
|
|
use Xetaravel\Livewire\Traits\WithSorting; |
|
21
|
|
|
use Xetaravel\Models\Badge as BadgeModel; |
|
22
|
|
|
|
|
23
|
|
|
class Badge extends Component |
|
24
|
|
|
{ |
|
25
|
|
|
use AuthorizesRequests; |
|
26
|
|
|
use Toastable; |
|
27
|
|
|
use WithBulkActions; |
|
|
|
|
|
|
28
|
|
|
use WithPagination; |
|
29
|
|
|
use WithPerPagePagination; |
|
30
|
|
|
use WithSorting; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* Bind the main model used in the component to be used in traits. |
|
34
|
|
|
* |
|
35
|
|
|
* @var string |
|
36
|
|
|
*/ |
|
37
|
|
|
public string $model = BadgeModel::class; |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* The field to sort by. |
|
41
|
|
|
* |
|
42
|
|
|
* @var string |
|
43
|
|
|
*/ |
|
44
|
|
|
#[Url(as: 'f', except: 'created_at')] |
|
45
|
|
|
public string $sortField = 'created_at'; |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* The direction of the ordering. |
|
49
|
|
|
* |
|
50
|
|
|
* @var string |
|
51
|
|
|
*/ |
|
52
|
|
|
#[Url(as: 'd')] |
|
53
|
|
|
public string $sortDirection = 'desc'; |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* The string to search. |
|
57
|
|
|
* |
|
58
|
|
|
* @var string |
|
59
|
|
|
*/ |
|
60
|
|
|
#[Url(as: 's', except: '')] |
|
61
|
|
|
public string $search = ''; |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* The number of permission limited per page. |
|
65
|
|
|
* |
|
66
|
|
|
* @var int |
|
67
|
|
|
*/ |
|
68
|
|
|
public int $perPage = 15; |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* Array of allowed fields. |
|
72
|
|
|
* |
|
73
|
|
|
* @var array |
|
74
|
|
|
*/ |
|
75
|
|
|
public array $allowedFields = [ |
|
76
|
|
|
'id', |
|
77
|
|
|
'name', |
|
78
|
|
|
'description', |
|
79
|
|
|
'type', |
|
80
|
|
|
'created_at' |
|
81
|
|
|
]; |
|
82
|
|
|
|
|
83
|
|
|
public function render(): View|Application|Factory|\Illuminate\View\View |
|
84
|
|
|
{ |
|
85
|
|
|
return view('livewire.admin.badge.badge', [ |
|
86
|
|
|
'badges' => $this->rows |
|
|
|
|
|
|
87
|
|
|
]); |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
/** |
|
91
|
|
|
* Create and return the query for the items. |
|
92
|
|
|
* |
|
93
|
|
|
* @return Builder |
|
94
|
|
|
*/ |
|
95
|
|
|
public function getRowsQueryProperty(): Builder |
|
96
|
|
|
{ |
|
97
|
|
|
$query = BadgeModel::query(); |
|
98
|
|
|
|
|
99
|
|
|
if (Gate::allows('search', BadgeModel::class)) { |
|
100
|
|
|
$query ->when($this->search, function ($query, $search) { |
|
101
|
|
|
return $query |
|
102
|
|
|
->where('name', 'LIKE', '%' . $search . '%') |
|
103
|
|
|
->orWhere('description', 'LIKE', '%' . $search . '%'); |
|
104
|
|
|
}); |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
return $this->applySorting($query); |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
/** |
|
111
|
|
|
* Build the query and paginate it. |
|
112
|
|
|
* |
|
113
|
|
|
* @return LengthAwarePaginator |
|
114
|
|
|
*/ |
|
115
|
|
|
public function getRowsProperty(): LengthAwarePaginator |
|
116
|
|
|
{ |
|
117
|
|
|
return $this->applyPagination($this->rowsQuery); |
|
|
|
|
|
|
118
|
|
|
} |
|
119
|
|
|
} |
|
120
|
|
|
|