Boolean Group field - inconsistency in recommended use #4962
-
|
Hello, in the Boolean Group field documentation section we see that it's recommended to cast underlying eloquent attribute to "array", but the data saved with this field is stored in the database as json object (my case: json type in mysql). When I have an eloquent attribute casted into array both reading and writing with this field works, but it's a bit confusing and inconsistent when, for example, in some class of my application writes data to this column for the first ime and I do it as follows (because the casts array in model points to an array): Then, it will write the data to the database as an array and the BooleanGroup field will not be able to handle the data - none of the BooleanGroup options will be selected, because this only works for the json object. So I have to write this data manually as a json object, but casts is recommended to be set as array. Maybe it's worth extending this field with a method that says whether to save the data as array or json? |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
|
You can override the default BooleanGroup::macro('fillAsObject', function () {
$this->fillUsing(function ($request, $model, $attribute, $requestAttribute) {
if ($request->exists($requestAttribute)) {
$model->{$attribute} = json_decode($request[$requestAttribute], false);
}
});
return $this;
}); |
Beta Was this translation helpful? Give feedback.
You can override the default
fillUsing()or create custom macro if you want to: