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