File tree Expand file tree Collapse file tree 1 file changed +21
-3
lines changed Expand file tree Collapse file tree 1 file changed +21
-3
lines changed Original file line number Diff line number Diff line change @@ -18,11 +18,29 @@ public function customFieldResponses()
1818 }
1919
2020 /**
21- * Save the given custom fields to the model.
21+ * Save the custom fields to the model.
22+ * This parameter accepts a list of fields for immediate saving & backwards compatibility.
2223 *
2324 * @param $fields
2425 */
25- public function saveCustomFields ($ fields )
26+ public function saveCustomFields ($ fields = [])
27+ {
28+ if (!empty ($ fields )) {
29+ $ this ->makeCustomFields ($ fields );
30+ }
31+
32+ $ this ->customFieldResponses ->each (function (CustomFieldResponse $ field ) {
33+ $ field ->model_id ??= $ this ->id ; // set the ID now, if model did not exist when makeCustomFields() was called
34+ $ field ->save ();
35+ });
36+ }
37+
38+ /**
39+ * Make the given custom fields but do not save.
40+ *
41+ * @param $fields
42+ */
43+ public function makeCustomFields ($ fields )
2644 {
2745 foreach ($ fields as $ key => $ value ) {
2846 $ customField = CustomField::find ((int ) $ key );
@@ -38,7 +56,7 @@ public function saveCustomFields($fields)
3856 'model_type ' => get_class ($ this ),
3957 ]);
4058
41- $ customFieldResponse -> save ( );
59+ $ this -> customFieldResponses -> push ( $ customFieldResponse );
4260 }
4361 }
4462
You can’t perform that action at this time.
0 commit comments