1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Rinvex\Forms\Models; |
6
|
|
|
|
7
|
|
|
use Illuminate\Database\Eloquent\Model; |
8
|
|
|
use Illuminate\Database\Eloquent\Builder; |
9
|
|
|
use Rinvex\Support\Traits\ValidatingTrait; |
10
|
|
|
use Illuminate\Database\Eloquent\Relations\BelongsTo; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* Rinvex\Forms\Models\FormResponse. |
14
|
|
|
* |
15
|
|
|
* @property int $id |
16
|
|
|
* @property string $unique_identifier |
17
|
|
|
* @property array $content |
18
|
|
|
* @property int $form_id |
19
|
|
|
* @property int $user_id |
20
|
|
|
* @property string $user_type |
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\Forms\Models\FormResponse ofUser(\Illuminate\Database\Eloquent\Model $user) |
27
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Forms\Models\FormResponse whereContent($value) |
28
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Forms\Models\FormResponse whereCreatedAt($value) |
29
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Forms\Models\FormResponse whereFormId($value) |
30
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Forms\Models\FormResponse whereUserId($value) |
31
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Forms\Models\FormResponse whereUserType($value) |
32
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Forms\Models\FormResponse whereUpdatedAt($value) |
33
|
|
|
* @mixin \Eloquent |
34
|
|
|
*/ |
35
|
|
|
class FormResponse extends Model |
36
|
|
|
{ |
37
|
|
|
use ValidatingTrait; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* {@inheritdoc} |
41
|
|
|
*/ |
42
|
|
|
protected $fillable = [ |
43
|
|
|
'unique_identifier', |
44
|
|
|
'content', |
45
|
|
|
'form_id', |
46
|
|
|
'user_id', |
47
|
|
|
'user_type', |
48
|
|
|
]; |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* {@inheritdoc} |
52
|
|
|
*/ |
53
|
|
|
protected $casts = [ |
54
|
|
|
'unique_identifier' => 'string', |
55
|
|
|
'content' => 'json', |
56
|
|
|
'form_id' => 'integer', |
57
|
|
|
'user_id' => 'integer', |
58
|
|
|
'user_type' => 'string', |
59
|
|
|
'deleted_at' => 'datetime', |
60
|
|
|
]; |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* {@inheritdoc} |
64
|
|
|
*/ |
65
|
|
|
protected $observables = [ |
66
|
|
|
'validating', |
67
|
|
|
'validated', |
68
|
|
|
]; |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* The default rules that the model will validate against. |
72
|
|
|
* |
73
|
|
|
* @var array |
74
|
|
|
*/ |
75
|
|
|
protected $rules = []; |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* Whether the model should throw a |
79
|
|
|
* ValidationException if it fails validation. |
80
|
|
|
* |
81
|
|
|
* @var bool |
82
|
|
|
*/ |
83
|
|
|
protected $throwValidationExceptions = true; |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* Create a new Eloquent model instance. |
87
|
|
|
* |
88
|
|
|
* @param array $attributes |
89
|
|
|
*/ |
90
|
|
|
public function __construct(array $attributes = []) |
91
|
|
|
{ |
92
|
|
|
parent::__construct($attributes); |
93
|
|
|
|
94
|
|
|
$this->setTable(config('rinvex.forms.tables.form_responses')); |
95
|
|
|
$this->setRules([ |
96
|
|
|
'unique_identifier' => 'nullable|string|strip_tags|max:150', |
97
|
|
|
'content' => 'required|array', |
98
|
|
|
'form_id' => 'required|integer|exists:'.config('rinvex.forms.tables.forms').',id', |
99
|
|
|
'user_id' => 'nullable|integer', |
100
|
|
|
'user_type' => 'nullable|string|strip_tags|max:150', |
101
|
|
|
]); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* Get the form response user. |
106
|
|
|
* |
107
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\MorphTo |
108
|
|
|
*/ |
109
|
|
|
public function user() |
110
|
|
|
{ |
111
|
|
|
return $this->morphTo('user', 'user_type', 'user_id', 'id'); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* Get form responses of the given user. |
116
|
|
|
* |
117
|
|
|
* @param \Illuminate\Database\Eloquent\Builder $builder |
118
|
|
|
* @param \Illuminate\Database\Eloquent\Model $user |
119
|
|
|
* |
120
|
|
|
* @return \Illuminate\Database\Eloquent\Builder |
121
|
|
|
*/ |
122
|
|
|
public function scopeOfUser(Builder $builder, Model $user): Builder |
123
|
|
|
{ |
124
|
|
|
return $builder->where('user_type', $user->getMorphClass())->where('user_id', $user->getKey()); |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* The form response always belongs to a form. |
129
|
|
|
* |
130
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo |
131
|
|
|
*/ |
132
|
|
|
public function form(): BelongsTo |
133
|
|
|
{ |
134
|
|
|
return $this->belongsTo(config('rinvex.forms.models.form'), 'form_id', 'id', 'form'); |
135
|
|
|
} |
136
|
|
|
} |
137
|
|
|
|