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