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