nova-flexible-content
nova-flexible-content copied to clipboard
Can't update models with any flexible field
I've recently upgraded to Laravel 11, and now I can't use FlexFields anymore.
Whenever I'm updating a model with any Flexfield, I get an Exception like this:
SQLSTATE[HY093]: Invalid parameter number (Connection: mysql, SQL: update `flexible_tests` set `content` = wysiwyg, `flexible_tests`.`updated_at` = c4Grl1AuNglB7p2r where `id` = My Title)
I've tried creating the most basic Model copying the docs to see of maybe something else goes wrong:
Model
class FlexibleTest extends Model
{
use HasFlexible;
protected function casts(): array
{
return [
'content' => FlexibleCast::class
];
}
}
Resource Fields:
public function fields(Request $request): array
{
return [
ID::make()->sortable(),
Flexible::make('Content')
->addLayout('Simple content section', 'wysiwyg', [
Text::make('Title'),
Markdown::make('Content')
])
->addLayout('Video section', 'video', [
Text::make('Title'),
Image::make('Video Thumbnail', 'thumbnail'),
Text::make('Video ID (YouTube)', 'video'),
Text::make('Video Caption', 'caption')
])
];
}
I want to add to this that it works fine when using sqlite and the error shows on MySql for me.
I've created a repo which shows the error.
There is a dusk TestCase in there which reproduces the error every time.
https://github.com/Jigsaw5279/flexible-content-demo
Facing the same bug, is there any solution, please?
I have the same problem.
I believe the cast just needs to json encode the result. E.g. add this to FlexibleCast or your own Cast that extends FlexibleCast:
use Illuminate\Database\Eloquent\Casts\Json;
public function set($model, string $key, $value, array $attributes)
{
return [$key => Json::encode($value)];
}
The above solution is insufficient. You still need to check whether the value is an iterable:
public function set($model, string $key, $value, array $attributes): mixed
{
if (is_iterable($value)) {
return [$key => json_encode($value)];
}
return $value;
}
Otherwise, things might start to break in unexpected ways.
Adding the following to your model file is sufficient:
protected $casts = [
'column_name' => 'array',
];
Replace 'column_name' with your actual column name in the database.
I want to add to this that it works fine when using sqlite and the error shows on MySql for me.
Thanks for this. Switched to sqlite and it worked.