1616 */
1717trait Customizable
1818{
19- protected ? array $ validatedCustomFieldValues = [];
19+ protected array $ validatedCustomFieldValues = [];
2020
2121 public static function bootCustomizable (): void
2222 {
@@ -37,21 +37,20 @@ public function customFieldValues(): MorphMany
3737 public function validateCustomFields (): void
3838 {
3939 $ customFields = $ this ->getCustomFields ();
40+ $ tableColumns = $ this ->getTableColumns ();
41+ $ modelAttributes = Collection::make ($ this ->getAttributes ());
42+ $ this ->setRawAttributes ($ modelAttributes ->only ($ tableColumns )->toArray ());
4043 if ($ customFields ->isEmpty ()) {
4144 return ;
4245 }
43- $ tableColumns = $ this ->getTableColumns ();
44- $ modelAttributes = Collection::make ($ this ->getAttributes ());
4546 $ modelAttributeKeys = $ modelAttributes ->keys ();
4647 $ customFieldColumns = $ modelAttributeKeys ->diff ($ tableColumns );
4748 $ customFieldsRules = $ customFields ->flatMap (function (CustomField $ field ) {
4849 return $ field ->getValidationRule ();
4950 })->all ();
50-
5151 $ customFieldValues = $ modelAttributes ->only ($ customFieldColumns )->toArray ();
5252 $ validator = Validator::make ($ customFieldValues , $ customFieldsRules );
5353 $ this ->validatedCustomFieldValues = $ validator ->validate ();
54- $ this ->setRawAttributes ($ modelAttributes ->only ($ tableColumns )->toArray ());
5554 }
5655
5756 protected function getTableColumns (): Collection
@@ -61,21 +60,23 @@ protected function getTableColumns(): Collection
6160
6261 public function getCustomFields (): Collection
6362 {
64- $ context = $ this ->getContext ();
63+ $ context = $ this ->getCustomFieldContext ();
6564 $ query = CustomField::query ();
65+ $ query ->where ('model_class ' , get_class ($ this ));
66+
6667 if (is_null ($ context )) {
6768 $ customFields = $ query ->get ();
6869 } else {
6970 $ customFields = $ query
70- ->where ('contextable_type ' , get_class ( $ context ))
71+ ->where ('contextable_type ' , $ context-> getMorphClass ( ))
7172 ->where ('contextable_id ' , $ context ->id )
7273 ->get ();
7374 }
7475
7576 return $ customFields ;
7677 }
7778
78- public function getContext (): ?Model
79+ public function getCustomFieldContext (): ?Model
7980 {
8081 return null ;
8182 }
@@ -92,7 +93,7 @@ public function saveCustomFieldValues(): void
9293 $ value = $ validatedCustomFieldValues [$ customField ->key ];
9394 $ constraints = [
9495 'custom_field_id ' => $ customField ->id ,
95- 'customizable_type ' => get_class ( $ this ),
96+ 'customizable_type ' => $ this -> getMorphClass ( ),
9697 'customizable_id ' => $ this ->id
9798 ];
9899 $ values = ['value ' => $ value ];
0 commit comments