99
1010class CustomField extends Model
1111{
12+ use SoftDeletes, HasFactory;
13+
14+ /**
15+ * @var string
16+ */
1217 const TYPE_CHECKBOX = 'checkbox ' ;
18+
19+ /**
20+ * @var string
21+ */
1322 const TYPE_NUMBER = 'number ' ;
23+
24+ /**
25+ * @var string
26+ */
1427 const TYPE_RADIO = 'radio ' ;
28+
29+ /**
30+ * @var string
31+ */
1532 const TYPE_SELECT = 'select ' ;
33+
34+ /**
35+ * @var string
36+ */
1637 const TYPE_TEXT = 'text ' ;
38+
39+ /**
40+ * @var string
41+ */
1742 const TYPE_TEXTAREA = 'textarea ' ;
1843
19- use SoftDeletes, HasFactory;
20-
44+ /**
45+ * The attributes that aren't mass assignable.
46+ *
47+ * @var string[]|bool
48+ */
2149 protected $ guarded = ['id ' ];
2250
51+ /**
52+ * The attributes that are mass assignable.
53+ *
54+ * @var string[]
55+ */
2356 protected $ fillable = [
24- 'type ' ,
25- 'title ' ,
26- 'description ' ,
27- 'answers ' ,
28- 'required ' ,
29- 'default_value ' ,
57+ 'type ' ,
58+ 'title ' ,
59+ 'description ' ,
60+ 'answers ' ,
61+ 'required ' ,
62+ 'default_value ' ,
3063 'order ' ,
3164 ];
3265
66+ /**
67+ * The attributes that should be cast.
68+ *
69+ * @var array
70+ */
3371 protected $ casts = [
3472 'answers ' => 'array ' ,
3573 ];
3674
75+ /**
76+ * CustomField constructor.
77+ *
78+ * @param array $attributes
79+ */
3780 public function __construct (array $ attributes = [])
3881 {
3982 parent ::__construct ($ attributes );
4083
4184 $ this ->table = config ('custom-fields.tables.fields ' , 'custom_fields ' );
4285 }
4386
44- private function fieldValidationRules ($ required )
87+ /**
88+ * Bootstrap the model and its traits.
89+ *
90+ * @return void
91+ */
92+ protected static function boot ()
93+ {
94+ parent ::boot ();
95+
96+ self ::creating (function ($ field ) {
97+ $ lastFieldOnCurrentModel = $ field ->model ->customFields ()->orderBy ('order ' , 'desc ' )->first ();
98+ $ field ->order = ($ lastFieldOnCurrentModel ? $ lastFieldOnCurrentModel ->order : 0 ) + 1 ;
99+ });
100+ }
101+
102+ /**
103+ * Get the morphable model.
104+ *
105+ * @return \Illuminate\Database\Eloquent\Relations\MorphTo
106+ */
107+ public function model ()
108+ {
109+ return $ this ->morphTo ();
110+ }
111+
112+ /**
113+ * Get the responses belonging to the model.
114+ *
115+ * @return \Illuminate\Database\Eloquent\Relations\HasMany
116+ */
117+ public function responses ()
118+ {
119+ return $ this ->hasMany (CustomFieldResponse::class, 'field_id ' );
120+ }
121+
122+ /**
123+ * Get the validation rules attribute.
124+ *
125+ * @return mixed
126+ */
127+ public function getValidationRulesAttribute ()
128+ {
129+ $ typeRules = $ this ->getFieldValidationRules ($ this ->required )[$ this ->type ];
130+ array_unshift ($ typeRules , $ this ->required ? 'required ' : 'nullable ' );
131+
132+ return $ typeRules ;
133+ }
134+
135+ /**
136+ * Get the field validation rules.
137+ *
138+ * @param $required
139+ * @return array
140+ */
141+ protected function getFieldValidationRules ($ required )
45142 {
46143 return [
47- self ::TYPE_CHECKBOX => $ required ? ['accepted ' ,'in:0,1 ' ] : ['in:0,1 ' ],
144+ self ::TYPE_CHECKBOX => $ required ? ['accepted ' , 'in:0,1 ' ] : ['in:0,1 ' ],
48145 self ::TYPE_NUMBER => [
49146 'integer ' ,
50147 ],
@@ -67,31 +164,4 @@ private function fieldValidationRules($required)
67164 ],
68165 ];
69166 }
70-
71- public function model ()
72- {
73- return $ this ->morphTo ();
74- }
75-
76- public function responses ()
77- {
78- return $ this ->hasMany (CustomFieldResponse::class, 'field_id ' );
79- }
80-
81- public function getValidationRulesAttribute ()
82- {
83- $ typeRules = $ this ->fieldValidationRules ($ this ->required )[$ this ->type ];
84- array_unshift ($ typeRules , $ this ->required ? 'required ' : 'nullable ' );
85-
86- return $ typeRules ;
87- }
88-
89- public static function boot ()
90- {
91- parent ::boot ();
92- self ::creating (function ($ field ) {
93- $ lastFieldOnCurrentModel = $ field ->model ->customFields ()->orderBy ('order ' , 'desc ' )->first ();
94- $ field ->order = ($ lastFieldOnCurrentModel ? $ lastFieldOnCurrentModel ->order : 0 ) + 1 ;
95- });
96- }
97167}
0 commit comments