1
|
|
|
<?php namespace Arcanesoft\Blog\Models; |
2
|
|
|
|
3
|
|
|
use Arcanedev\Localization\Traits\HasTranslations; |
4
|
|
|
use Arcanesoft\Blog\Blog; |
5
|
|
|
use Illuminate\Database\Eloquent\SoftDeletes; |
6
|
|
|
use Illuminate\Support\Facades\Cache; |
7
|
|
|
use Illuminate\Support\Str; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Class Category |
11
|
|
|
* |
12
|
|
|
* @package Arcanesoft\Blog\Models |
13
|
|
|
* @author ARCANEDEV <[email protected]> |
14
|
|
|
* |
15
|
|
|
* @property int id |
16
|
|
|
* @property string name |
17
|
|
|
* @property string slug |
18
|
|
|
* @property \Carbon\Carbon created_at |
19
|
|
|
* @property \Carbon\Carbon updated_at |
20
|
|
|
* @property \Carbon\Carbon deleted_at |
21
|
|
|
* |
22
|
|
|
* @property \Illuminate\Database\Eloquent\Collection posts |
23
|
|
|
*/ |
24
|
|
|
class Category extends AbstractModel |
25
|
|
|
{ |
26
|
|
|
/* ----------------------------------------------------------------- |
27
|
|
|
| Traits |
28
|
|
|
| ----------------------------------------------------------------- |
29
|
|
|
*/ |
30
|
|
|
|
31
|
|
|
use SoftDeletes, |
32
|
|
|
HasTranslations; |
33
|
|
|
|
34
|
|
|
/* ----------------------------------------------------------------- |
35
|
|
|
| Properties |
36
|
|
|
| ----------------------------------------------------------------- |
37
|
|
|
*/ |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* The database table used by the model |
41
|
|
|
* |
42
|
|
|
* @var string |
43
|
|
|
*/ |
44
|
|
|
protected $table = 'categories'; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* The attributes that are mass assignable |
48
|
|
|
* |
49
|
|
|
* @var array |
50
|
|
|
*/ |
51
|
|
|
protected $fillable = ['name', 'slug']; |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* The attributes that should be mutated to dates. |
55
|
|
|
* |
56
|
|
|
* @var array |
57
|
|
|
*/ |
58
|
|
|
protected $dates = ['deleted_at']; |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* The attributes that should be cast to native types. |
62
|
|
|
* |
63
|
|
|
* @var array |
64
|
|
|
*/ |
65
|
|
|
protected $casts = [ |
66
|
|
|
'id' => 'integer', |
67
|
|
|
]; |
68
|
|
|
|
69
|
|
|
/* ----------------------------------------------------------------- |
70
|
|
|
| Relationships |
71
|
|
|
| ----------------------------------------------------------------- |
72
|
|
|
*/ |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Relationship with posts. |
76
|
|
|
* |
77
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\HasMany |
78
|
|
|
*/ |
79
|
|
|
public function posts() |
80
|
|
|
{ |
81
|
|
|
return $this->hasMany(Post::class); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/* ----------------------------------------------------------------- |
85
|
|
|
| Getters & Setters |
86
|
|
|
| ----------------------------------------------------------------- |
87
|
|
|
*/ |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* Set the name attribute. |
91
|
|
|
* |
92
|
|
|
* @param string $name |
93
|
|
|
* |
94
|
|
|
* @return string |
95
|
|
|
*/ |
96
|
|
|
public function setNameAttribute($name) |
97
|
|
|
{ |
98
|
|
|
return $this->attributes['name'] = $name; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* Set the slug attribute. |
103
|
|
|
* |
104
|
|
|
* @param string $name |
105
|
|
|
* |
106
|
|
|
* @return string |
107
|
|
|
*/ |
108
|
|
|
public function setSlugAttribute($name) |
109
|
|
|
{ |
110
|
|
|
return $this->attributes['slug'] = Str::slug($name); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* Get the translatable attributes. |
115
|
|
|
* |
116
|
|
|
* @return array |
117
|
|
|
*/ |
118
|
|
|
public function getTranslatableAttributes() |
119
|
|
|
{ |
120
|
|
|
return Blog::instance()->isTranslatable() ? ['name', 'slug'] : []; |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/* ----------------------------------------------------------------- |
124
|
|
|
| Main Methods |
125
|
|
|
| ----------------------------------------------------------------- |
126
|
|
|
*/ |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* Create a new category. |
130
|
|
|
* |
131
|
|
|
* @param array $attributes |
132
|
|
|
* |
133
|
|
|
* @return self |
134
|
|
|
*/ |
135
|
|
|
public static function createOne(array $attributes) |
136
|
|
|
{ |
137
|
|
|
$category = new self($attributes); |
138
|
|
|
$category->save(); |
139
|
|
|
|
140
|
|
|
return $category; |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* Update the current category. |
145
|
|
|
* |
146
|
|
|
* @param array $attributes |
147
|
|
|
* |
148
|
|
|
* @return self |
149
|
|
|
*/ |
150
|
|
|
public function updateOne(array $attributes) |
151
|
|
|
{ |
152
|
|
|
$this->fill($attributes)->save(); |
153
|
|
|
|
154
|
|
|
return $this; |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
/** |
158
|
|
|
* Get the categories options for select input. |
159
|
|
|
* |
160
|
|
|
* @param bool $placeholder |
161
|
|
|
* |
162
|
|
|
* @return \Illuminate\Database\Eloquent\Collection |
163
|
|
|
*/ |
164
|
|
|
public static function getSelectOptions($placeholder = true) |
165
|
|
|
{ |
166
|
|
|
/** @var \Illuminate\Database\Eloquent\Collection $categories */ |
167
|
|
|
$categories = Cache::remember('blog_categories_select_options', 5, function () { |
168
|
|
|
return self::all()->keyBy('id')->transform(function (Category $category) { |
169
|
|
|
return Blog::instance()->isTranslatable() |
170
|
|
|
? implode(' / ', $category->getTranslations('name')) |
171
|
|
|
: $category->name; |
172
|
|
|
}); |
173
|
|
|
}); |
174
|
|
|
|
175
|
|
|
return $placeholder |
176
|
|
|
? $categories->prepend(trans('blog::categories.select-category'), 0) |
177
|
|
|
: $categories; |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
/* ----------------------------------------------------------------- |
181
|
|
|
| Check Methods |
182
|
|
|
| ----------------------------------------------------------------- |
183
|
|
|
*/ |
184
|
|
|
|
185
|
|
|
/** |
186
|
|
|
* Check if category has posts. |
187
|
|
|
* |
188
|
|
|
* @return bool |
189
|
|
|
*/ |
190
|
|
|
public function hasPosts() |
191
|
|
|
{ |
192
|
|
|
return ! $this->posts->isEmpty(); |
193
|
|
|
} |
194
|
|
|
|
195
|
|
|
/** |
196
|
|
|
* Check if the category is deletable. |
197
|
|
|
* |
198
|
|
|
* @return bool |
199
|
|
|
*/ |
200
|
|
|
public function isDeletable() |
201
|
|
|
{ |
202
|
|
|
return ! $this->hasPosts(); |
203
|
|
|
} |
204
|
|
|
|
205
|
|
|
/* ----------------------------------------------------------------- |
206
|
|
|
| Other Methods |
207
|
|
|
| ----------------------------------------------------------------- |
208
|
|
|
*/ |
209
|
|
|
|
210
|
|
|
/** |
211
|
|
|
* Clear the cached categories. |
212
|
|
|
*/ |
213
|
|
|
public static function clearCache() |
214
|
|
|
{ |
215
|
|
|
cache()->forget('blog_categories_select_options'); |
216
|
|
|
} |
217
|
|
|
|
218
|
|
|
/** |
219
|
|
|
* Fill the model with an array of attributes. |
220
|
|
|
* |
221
|
|
|
* @param array $attributes |
222
|
|
|
* |
223
|
|
|
* @return self |
224
|
|
|
*/ |
225
|
9 |
|
public function fill(array $attributes) |
226
|
|
|
{ |
227
|
9 |
|
if ( ! Blog::instance()->isTranslatable()) |
228
|
3 |
|
return parent::fill($attributes); |
229
|
|
|
|
230
|
9 |
|
$this->setTranslations('name', $attributes['name']) |
231
|
|
|
->setTranslations('slug', $attributes['name']); |
232
|
|
|
|
233
|
|
|
return $this; |
234
|
|
|
} |
235
|
|
|
} |
236
|
|
|
|