Skip to content

Commit 45eeff6

Browse files
committed
ADDED: Ability to makeCustomFields() and then call saveCustomFields() to commit the data at a later point in time.
1 parent 92b7de2 commit 45eeff6

File tree

1 file changed

+21
-3
lines changed

1 file changed

+21
-3
lines changed

src/Traits/HasCustomFieldResponses.php

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff 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

0 commit comments

Comments
 (0)