1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Rinvex\Categories\Models; |
6
|
|
|
|
7
|
|
|
use Kalnoy\Nestedset\NestedSet; |
8
|
|
|
use Kalnoy\Nestedset\NodeTrait; |
9
|
|
|
use Spatie\Sluggable\SlugOptions; |
10
|
|
|
use Rinvex\Support\Traits\HasSlug; |
11
|
|
|
use Illuminate\Database\Eloquent\Model; |
12
|
|
|
use Rinvex\Support\Traits\HasTranslations; |
13
|
|
|
use Rinvex\Support\Traits\ValidatingTrait; |
14
|
|
|
use Illuminate\Database\Eloquent\SoftDeletes; |
15
|
|
|
use Illuminate\Database\Eloquent\Relations\MorphToMany; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Rinvex\Categories\Models\Category. |
19
|
|
|
* |
20
|
|
|
* @property int $id |
21
|
|
|
* @property string $slug |
22
|
|
|
* @property array $name |
23
|
|
|
* @property array $description |
24
|
|
|
* @property int $_lft |
25
|
|
|
* @property int $_rgt |
26
|
|
|
* @property int $parent_id |
27
|
|
|
* @property \Carbon\Carbon|null $created_at |
28
|
|
|
* @property \Carbon\Carbon|null $updated_at |
29
|
|
|
* @property \Carbon\Carbon|null $deleted_at |
30
|
|
|
* @property-read \Kalnoy\Nestedset\Collection|\Rinvex\Categories\Models\Category[] $children |
31
|
|
|
* @property-read \Rinvex\Categories\Models\Category|null $parent |
32
|
|
|
* |
33
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Categories\Models\Category whereCreatedAt($value) |
34
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Categories\Models\Category whereDeletedAt($value) |
35
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Categories\Models\Category whereDescription($value) |
36
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Categories\Models\Category whereId($value) |
37
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Categories\Models\Category whereLft($value) |
38
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Categories\Models\Category whereName($value) |
39
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Categories\Models\Category whereParentId($value) |
40
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Categories\Models\Category whereRgt($value) |
41
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Categories\Models\Category whereSlug($value) |
42
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Categories\Models\Category whereUpdatedAt($value) |
43
|
|
|
* @mixin \Eloquent |
44
|
|
|
*/ |
45
|
|
|
class Category extends Model |
46
|
|
|
{ |
47
|
|
|
use HasSlug; |
48
|
|
|
use NodeTrait; |
49
|
|
|
use SoftDeletes; |
50
|
|
|
use HasTranslations; |
51
|
|
|
use ValidatingTrait; |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* {@inheritdoc} |
55
|
|
|
*/ |
56
|
|
|
protected $fillable = [ |
57
|
|
|
'slug', |
58
|
|
|
'name', |
59
|
|
|
'description', |
60
|
|
|
NestedSet::LFT, |
61
|
|
|
NestedSet::RGT, |
62
|
|
|
NestedSet::PARENT_ID, |
63
|
|
|
]; |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* {@inheritdoc} |
67
|
|
|
*/ |
68
|
|
|
protected $casts = [ |
69
|
|
|
'slug' => 'string', |
70
|
|
|
NestedSet::LFT => 'integer', |
71
|
|
|
NestedSet::RGT => 'integer', |
72
|
|
|
NestedSet::PARENT_ID => 'integer', |
73
|
|
|
'deleted_at' => 'datetime', |
74
|
|
|
]; |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* {@inheritdoc} |
78
|
|
|
*/ |
79
|
|
|
protected $observables = [ |
80
|
|
|
'validating', |
81
|
|
|
'validated', |
82
|
|
|
]; |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* The attributes that are translatable. |
86
|
|
|
* |
87
|
|
|
* @var array |
88
|
|
|
*/ |
89
|
|
|
public $translatable = [ |
90
|
|
|
'name', |
91
|
|
|
'description', |
92
|
|
|
]; |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* The default rules that the model will validate against. |
96
|
|
|
* |
97
|
|
|
* @var array |
98
|
|
|
*/ |
99
|
|
|
protected $rules = []; |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* Whether the model should throw a |
103
|
|
|
* ValidationException if it fails validation. |
104
|
|
|
* |
105
|
|
|
* @var bool |
106
|
|
|
*/ |
107
|
|
|
protected $throwValidationExceptions = true; |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* Create a new Eloquent model instance. |
111
|
|
|
* |
112
|
|
|
* @param array $attributes |
113
|
|
|
*/ |
114
|
|
|
public function __construct(array $attributes = []) |
115
|
|
|
{ |
116
|
|
|
$this->setTable(config('rinvex.categories.tables.categories')); |
117
|
|
|
$this->mergeRules([ |
118
|
|
|
'name' => 'required|string|strip_tags|max:150', |
119
|
|
|
'description' => 'nullable|string|max:32768', |
120
|
|
|
'slug' => 'required|alpha_dash|max:150|unique:'.config('rinvex.categories.tables.categories').',slug', |
121
|
|
|
NestedSet::LFT => 'sometimes|required|integer', |
122
|
|
|
NestedSet::RGT => 'sometimes|required|integer', |
123
|
|
|
NestedSet::PARENT_ID => 'nullable|integer', |
124
|
|
|
]); |
125
|
|
|
|
126
|
|
|
parent::__construct($attributes); |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* Get all attached models of the given class to the category. |
131
|
|
|
* |
132
|
|
|
* @param string $class |
133
|
|
|
* |
134
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\MorphToMany |
135
|
|
|
*/ |
136
|
|
|
public function entries(string $class): MorphToMany |
137
|
|
|
{ |
138
|
|
|
return $this->morphedByMany($class, 'categorizable', config('rinvex.categories.tables.categorizables'), 'category_id', 'categorizable_id', 'id', 'id'); |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* Get the options for generating the slug. |
143
|
|
|
* |
144
|
|
|
* @return \Spatie\Sluggable\SlugOptions |
145
|
|
|
*/ |
146
|
|
|
public function getSlugOptions(): SlugOptions |
147
|
|
|
{ |
148
|
|
|
return SlugOptions::create() |
149
|
|
|
->doNotGenerateSlugsOnUpdate() |
150
|
|
|
->generateSlugsFrom('name') |
151
|
|
|
->saveSlugsTo('slug'); |
152
|
|
|
} |
153
|
|
|
} |
154
|
|
|
|