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