Passed
Push — 5.0.0 ( 757365...6c4e14 )
by Fèvre
05:11
created

Category::render()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 4
rs 10
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;
0 ignored issues
show
introduced by
The trait Xetaravel\Livewire\Traits\WithBulkActions requires some properties which are not provided by Xetaravel\Livewire\Admin\Blog\Category: $selectedRowsQuery, $rows
Loading history...
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
0 ignored issues
show
Bug Best Practice introduced by
The property rows does not exist on Xetaravel\Livewire\Admin\Blog\Category. Since you implemented __get, consider adding a @property annotation.
Loading history...
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);
0 ignored issues
show
Bug introduced by
$query of type Xetaravel\Models\BlogCategory is incompatible with the type Illuminate\Contracts\Database\Query\Builder expected by parameter $query of Xetaravel\Livewire\Admin...ategory::applySorting(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

106
        return $this->applySorting(/** @scrutinizer ignore-type */ $query);
Loading history...
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);
0 ignored issues
show
Bug Best Practice introduced by
The property rowsQuery does not exist on Xetaravel\Livewire\Admin\Blog\Category. Since you implemented __get, consider adding a @property annotation.
Loading history...
117
    }
118
}
119