|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace App\Models; |
|
4
|
|
|
|
|
5
|
|
|
use App\Http\Resources\TestimonialResource; |
|
6
|
|
|
use App\Traits\Hashidable; |
|
7
|
|
|
use Illuminate\Database\Eloquent\Builder; |
|
8
|
|
|
use Illuminate\Database\Eloquent\Factories\HasFactory; |
|
9
|
|
|
use Illuminate\Database\Eloquent\Model; |
|
10
|
|
|
use Illuminate\Support\Facades\Storage; |
|
11
|
|
|
use Spatie\MediaLibrary\HasMedia; |
|
12
|
|
|
use Spatie\MediaLibrary\InteractsWithMedia; |
|
13
|
|
|
use Spatie\MediaLibrary\MediaCollections\Models\Media as SpatieMedia; |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* App\Models\Testimonial. |
|
17
|
|
|
* |
|
18
|
|
|
* @property int $id |
|
19
|
|
|
* @property string $name |
|
20
|
|
|
* @property string $email |
|
21
|
|
|
* @property string $position |
|
22
|
|
|
* @property string $body |
|
23
|
|
|
* @property int $stars |
|
24
|
|
|
* @property bool $is_published |
|
25
|
|
|
* @property \Illuminate\Support\Carbon|null $created_at |
|
26
|
|
|
* @property \Illuminate\Support\Carbon|null $updated_at |
|
27
|
|
|
* @property string $avatar |
|
28
|
|
|
* @property-read string $avatar_thumb |
|
29
|
|
|
* @property-read string $hashed_id |
|
30
|
|
|
* @property-read \Spatie\MediaLibrary\MediaCollections\Models\Collections\MediaCollection|\App\Models\Media[] $media |
|
31
|
|
|
* @property-read int|null $media_count |
|
32
|
|
|
* @method static Builder|Testimonial findByHashedId(string $hashedId) |
|
33
|
|
|
* @method static Builder|Testimonial newModelQuery() |
|
34
|
|
|
* @method static Builder|Testimonial newQuery() |
|
35
|
|
|
* @method static Builder|Testimonial published() |
|
36
|
|
|
* @method static Builder|Testimonial query() |
|
37
|
|
|
* @method static Builder|Testimonial whereBody($value) |
|
38
|
|
|
* @method static Builder|Testimonial whereCreatedAt($value) |
|
39
|
|
|
* @method static Builder|Testimonial whereEmail($value) |
|
40
|
|
|
* @method static Builder|Testimonial whereId($value) |
|
41
|
|
|
* @method static Builder|Testimonial whereIsPublished($value) |
|
42
|
|
|
* @method static Builder|Testimonial whereName($value) |
|
43
|
|
|
* @method static Builder|Testimonial wherePosition($value) |
|
44
|
|
|
* @method static Builder|Testimonial whereStars($value) |
|
45
|
|
|
* @method static Builder|Testimonial whereUpdatedAt($value) |
|
46
|
|
|
* @mixin \Eloquent |
|
47
|
|
|
*/ |
|
48
|
|
|
class Testimonial extends Model implements HasMedia |
|
49
|
|
|
{ |
|
50
|
|
|
use HasFactory, Hashidable, InteractsWithMedia; |
|
|
|
|
|
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* The table associated with the model. |
|
54
|
|
|
* |
|
55
|
|
|
* @var string |
|
56
|
|
|
*/ |
|
57
|
|
|
protected $table = 'testimonials'; |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* The attributes that are mass assignable. |
|
61
|
|
|
* |
|
62
|
|
|
* @var array |
|
63
|
|
|
*/ |
|
64
|
|
|
protected $fillable = [ |
|
65
|
|
|
'avatar', |
|
66
|
|
|
'name', |
|
67
|
|
|
'email', |
|
68
|
|
|
'position', |
|
69
|
|
|
'body', |
|
70
|
|
|
'stars', |
|
71
|
|
|
'is_published', |
|
72
|
|
|
]; |
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* The accessors to append to the model's array form. |
|
76
|
|
|
* |
|
77
|
|
|
* @var array |
|
78
|
|
|
*/ |
|
79
|
|
|
protected $appends = ['hashed_id']; |
|
80
|
|
|
|
|
81
|
|
|
/** |
|
82
|
|
|
* The attributes that should be cast to native types. |
|
83
|
|
|
* |
|
84
|
|
|
* @var array |
|
85
|
|
|
*/ |
|
86
|
|
|
protected $casts = [ |
|
87
|
|
|
'is_published' => 'boolean', |
|
88
|
|
|
'stars' => 'integer', |
|
89
|
|
|
]; |
|
90
|
|
|
|
|
91
|
|
|
/* |
|
92
|
|
|
* ----------------------------------------------------------------- * |
|
93
|
|
|
* --------------------------- Accessors --------------------------- * |
|
94
|
|
|
* ----------------------------------------------------------------- * |
|
95
|
|
|
*/ |
|
96
|
|
|
|
|
97
|
|
|
/** |
|
98
|
|
|
* Retrieve creator avatar attribute. |
|
99
|
|
|
* |
|
100
|
|
|
* @return string |
|
101
|
|
|
*/ |
|
102
|
|
|
public function getAvatarAttribute() |
|
103
|
|
|
{ |
|
104
|
|
|
return ! blank($media = $this->getFirstMediaUrl('avatar', 'big')) ? $media : url(Storage::url('public/avatar.jpg')); |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
/** |
|
108
|
|
|
* Retrieve creator avatar_thumb attribute. |
|
109
|
|
|
* |
|
110
|
|
|
* @return string |
|
111
|
|
|
*/ |
|
112
|
|
|
public function getAvatarThumbAttribute() |
|
113
|
|
|
{ |
|
114
|
|
|
return ! blank($media = $this->getFirstMediaUrl('avatar', 'thumb')) ? $media : url(Storage::url('public/avatar.jpg')); |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
/* |
|
118
|
|
|
* ----------------------------------------------------------------- * |
|
119
|
|
|
* ---------------------------- Mutators --------------------------- * |
|
120
|
|
|
* ----------------------------------------------------------------- * |
|
121
|
|
|
*/ |
|
122
|
|
|
|
|
123
|
|
|
/** |
|
124
|
|
|
* Set creator avatar attribute. |
|
125
|
|
|
* |
|
126
|
|
|
* @param mixed $avatar |
|
127
|
|
|
* @return void |
|
128
|
|
|
*/ |
|
129
|
|
|
public function setAvatarAttribute($avatar) |
|
130
|
|
|
{ |
|
131
|
|
|
$avatar = is_string($avatar) ? $this->addMediaFromUrl($avatar) : $this->addMedia($avatar); |
|
132
|
|
|
|
|
133
|
|
|
$avatar->setName("{$this->hashed_id}_avatar") |
|
134
|
|
|
->setFileName("{$this->hashed_id}-avatar") |
|
135
|
|
|
->toMediaCollection('avatar'); |
|
136
|
|
|
} |
|
137
|
|
|
|
|
138
|
|
|
/* |
|
139
|
|
|
* ----------------------------------------------------------------- * |
|
140
|
|
|
* ----------------------------- Scopes ---------------------------- * |
|
141
|
|
|
* ----------------------------------------------------------------- * |
|
142
|
|
|
*/ |
|
143
|
|
|
|
|
144
|
|
|
/** |
|
145
|
|
|
* Scope to get published testimonials. |
|
146
|
|
|
* |
|
147
|
|
|
* @param \Illuminate\Database\Eloquent\Builder $builder |
|
148
|
|
|
* @return \Illuminate\Database\Eloquent\Builder |
|
149
|
|
|
*/ |
|
150
|
|
|
public function scopePublished(Builder $query): Builder |
|
151
|
|
|
{ |
|
152
|
|
|
return $query->where('is_published', true); |
|
153
|
|
|
} |
|
154
|
|
|
|
|
155
|
|
|
/* |
|
156
|
|
|
* ----------------------------------------------------------------- * |
|
157
|
|
|
* ------------------------------ Misc ----------------------------- * |
|
158
|
|
|
* ----------------------------------------------------------------- * |
|
159
|
|
|
*/ |
|
160
|
|
|
|
|
161
|
|
|
/** |
|
162
|
|
|
* Register the media collections. |
|
163
|
|
|
* |
|
164
|
|
|
* @return void |
|
165
|
|
|
*/ |
|
166
|
|
|
public function registerMediaCollections(): void |
|
167
|
|
|
{ |
|
168
|
|
|
$this->addMediaCollection('avatar') |
|
169
|
|
|
->singleFile(); |
|
170
|
|
|
} |
|
171
|
|
|
|
|
172
|
|
|
/** |
|
173
|
|
|
* Register media conversions. |
|
174
|
|
|
* |
|
175
|
|
|
* @param Media $media |
|
176
|
|
|
*/ |
|
177
|
|
|
public function registerMediaConversions(SpatieMedia $media = null): void |
|
178
|
|
|
{ |
|
179
|
|
|
$this->addMediaConversion('thumb') |
|
180
|
|
|
->fit('crop', 100, 100) |
|
181
|
|
|
->performOnCollections('avatar'); |
|
182
|
|
|
|
|
183
|
|
|
// Perform a resize on every collection |
|
184
|
|
|
$this->addMediaConversion('big') |
|
185
|
|
|
->fit('crop', 500, 500) |
|
186
|
|
|
->performOnCollections('avatar'); |
|
187
|
|
|
} |
|
188
|
|
|
|
|
189
|
|
|
/** |
|
190
|
|
|
* Convert the model instance to an array. |
|
191
|
|
|
* |
|
192
|
|
|
* @return array |
|
193
|
|
|
*/ |
|
194
|
|
|
public function toArray() |
|
195
|
|
|
{ |
|
196
|
|
|
return TestimonialResource::make($this)->toArray(request()); |
|
197
|
|
|
} |
|
198
|
|
|
} |
|
199
|
|
|
|