nova-flexible-content icon indicating copy to clipboard operation
nova-flexible-content copied to clipboard

Can't update models with any flexible field

Open Jigsaw5279 opened this issue 1 year ago • 8 comments

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')
                ])
        ];
    }

image

Jigsaw5279 avatar Apr 12 '24 19:04 Jigsaw5279

I want to add to this that it works fine when using sqlite and the error shows on MySql for me.

MohammadAbusaleh avatar Apr 14 '24 14:04 MohammadAbusaleh

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

failure-Tests_Browser_FlexibleContentTest_userCanBeEdited-0

Jigsaw5279 avatar Apr 14 '24 16:04 Jigsaw5279

Facing the same bug, is there any solution, please?

tursunbaevz avatar Apr 15 '24 17:04 tursunbaevz

I have the same problem.

Frozenpath avatar Apr 16 '24 05:04 Frozenpath

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)];
}

ryzr avatar Apr 18 '24 03:04 ryzr

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.

mabdullahsari avatar May 29 '24 14:05 mabdullahsari

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.

DylanS94 avatar Jun 03 '24 07:06 DylanS94

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.

AvishkaSen avatar Oct 04 '24 07:10 AvishkaSen