Flexible content column is reset after updating model externally
francescomalatesta opened this issue ยท 1 comments
Hi everyone! I am going crazy with this one; I hope you can help me ๐
I am working with Laravel 10 and Nova 4. I have a model called Pipeline that I manage with Nova for CRUD operations. One of its fields is called sources, and I used FlexibleContent to make it... flexible :D
I am using the suggested cast in the model:
protected $casts = [
'sources' => FlexibleCast::class
];
When I work with Nova, everything is alright. Here's an example of a value I see in my column.
[{"key": "cIKVNekjrJmVVbYI", "layout": "source", "attributes": {"source_id": "99204b10-74fd-42a3-858b-3905e5338a42", "attempts": 3}}]
Sometimes I need to edit this model. Not the sources field, that is always and only managed by Nova, but other unrelated fields. Whenever I make these edits and save the model, the value in sources appears like it was reset:
[{"name": "source", "limit": null, "title": "source", "fields": []}]
Just to give you an idea, the code making the edit is like
$pipeline = Pipeline::query()->find($id);
$pipeline->last_attempt_at = now();
$pipeline->save();
As you can see, the sources field is not mentioned at all. But after that save call, its value is changed (in a breaking way for my code).
Every tip/hint is welcome ๐ Thanks a lot for your time!
Did you figure this out?
Edit:
I've added the following to my CustomFlexibleCast
and this appears to temporarily fix the issue:
public function set($model, string $key, $value, array $attributes)
{
$serializedBlocks = Collection::wrap($value)->map(function ($block) {
return is_array($block) ? $block : [
'layout' => $block->name(),
'key' => $block->inUseKey(),
'attributes' => $block->getAttributes()
];
})->toArray();
return json_encode($serializedBlocks);
}