1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Rinvex\Testimonials\Models; |
6
|
|
|
|
7
|
|
|
use Illuminate\Database\Eloquent\Model; |
8
|
|
|
use Rinvex\Cacheable\CacheableEloquent; |
9
|
|
|
use Illuminate\Database\Eloquent\Builder; |
10
|
|
|
use Rinvex\Support\Traits\ValidatingTrait; |
11
|
|
|
use Illuminate\Database\Eloquent\Relations\BelongsTo; |
12
|
|
|
use Rinvex\Testimonials\Contracts\TestimonialContract; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Rinvex\Testimonials\Models\Testimonial. |
16
|
|
|
* |
17
|
|
|
* @property int $id |
18
|
|
|
* @property int $user_id |
19
|
|
|
* @property bool $is_approved |
20
|
|
|
* @property string $body |
21
|
|
|
* @property \Carbon\Carbon|null $created_at |
22
|
|
|
* @property \Carbon\Carbon|null $updated_at |
23
|
|
|
* @property \Carbon\Carbon|null $deleted_at |
24
|
|
|
* @property-read \Illuminate\Database\Eloquent\Model|\Eloquent $user |
25
|
|
|
* |
26
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Testimonials\Models\Testimonial approved() |
27
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Testimonials\Models\Testimonial disapproved() |
28
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Testimonials\Models\Testimonial whereBody($value) |
29
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Testimonials\Models\Testimonial whereCreatedAt($value) |
30
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Testimonials\Models\Testimonial whereDeletedAt($value) |
31
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Testimonials\Models\Testimonial whereId($value) |
32
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Testimonials\Models\Testimonial whereIsApproved($value) |
33
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Testimonials\Models\Testimonial whereUpdatedAt($value) |
34
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Testimonials\Models\Testimonial whereUserId($value) |
35
|
|
|
* @mixin \Eloquent |
36
|
|
|
*/ |
37
|
|
|
class Testimonial extends Model implements TestimonialContract |
38
|
|
|
{ |
39
|
|
|
use ValidatingTrait; |
40
|
|
|
use CacheableEloquent; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* {@inheritdoc} |
44
|
|
|
*/ |
45
|
|
|
protected $fillable = [ |
46
|
|
|
'user_id', |
47
|
|
|
'is_approved', |
48
|
|
|
'body', |
49
|
|
|
]; |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* {@inheritdoc} |
53
|
|
|
*/ |
54
|
|
|
protected $casts = [ |
55
|
|
|
'user_id' => 'integer', |
56
|
|
|
'is_approved' => 'boolean', |
57
|
|
|
'body' => 'string', |
58
|
|
|
'deleted_at' => 'datetime', |
59
|
|
|
]; |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* {@inheritdoc} |
63
|
|
|
*/ |
64
|
|
|
protected $observables = [ |
65
|
|
|
'validating', |
66
|
|
|
'validated', |
67
|
|
|
]; |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* The default rules that the model will validate against. |
71
|
|
|
* |
72
|
|
|
* @var array |
73
|
|
|
*/ |
74
|
|
|
protected $rules = [ |
75
|
|
|
'user_id' => 'required|integer', |
76
|
|
|
'is_approved' => 'sometimes|boolean', |
77
|
|
|
'body' => 'required|string|max:150', |
78
|
|
|
]; |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* Whether the model should throw a |
82
|
|
|
* ValidationException if it fails validation. |
83
|
|
|
* |
84
|
|
|
* @var bool |
85
|
|
|
*/ |
86
|
|
|
protected $throwValidationExceptions = true; |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* Create a new Eloquent model instance. |
90
|
|
|
* |
91
|
|
|
* @param array $attributes |
92
|
|
|
*/ |
93
|
|
|
public function __construct(array $attributes = []) |
94
|
|
|
{ |
95
|
|
|
parent::__construct($attributes); |
96
|
|
|
|
97
|
|
|
$this->setTable(config('rinvex.testimonials.tables.testimonials')); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* Get the approved testimonials. |
102
|
|
|
* |
103
|
|
|
* @param \Illuminate\Database\Eloquent\Builder $builder |
104
|
|
|
* |
105
|
|
|
* @return \Illuminate\Database\Eloquent\Builder |
106
|
|
|
*/ |
107
|
|
|
public function scopeApproved(Builder $builder): Builder |
108
|
|
|
{ |
109
|
|
|
return $builder->where('is_approved', true); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* Get the disapproved testimonials. |
114
|
|
|
* |
115
|
|
|
* @param \Illuminate\Database\Eloquent\Builder $builder |
116
|
|
|
* |
117
|
|
|
* @return \Illuminate\Database\Eloquent\Builder |
118
|
|
|
*/ |
119
|
|
|
public function scopeDisapproved(Builder $builder): Builder |
120
|
|
|
{ |
121
|
|
|
return $builder->where('is_approved', false); |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* Get the owner model of the testimonial. |
126
|
|
|
* |
127
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo |
128
|
|
|
*/ |
129
|
|
|
public function user(): belongsTo |
130
|
|
|
{ |
131
|
|
|
return $this->belongsTo(config('auth.providers.users.model'), 'user_id', 'id'); |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* Approve the testimonial. |
136
|
|
|
* |
137
|
|
|
* @return $this |
138
|
|
|
*/ |
139
|
|
|
public function approve() |
140
|
|
|
{ |
141
|
|
|
$this->update(['is_approved' => true]); |
142
|
|
|
|
143
|
|
|
return $this; |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
/** |
147
|
|
|
* Disapprove the testimonial. |
148
|
|
|
* |
149
|
|
|
* @return $this |
150
|
|
|
*/ |
151
|
|
|
public function disapprove() |
152
|
|
|
{ |
153
|
|
|
$this->update(['is_approved' => false]); |
154
|
|
|
|
155
|
|
|
return $this; |
156
|
|
|
} |
157
|
|
|
} |
158
|
|
|
|