whitecube / nova-flexible-content

Flexible Content & Repeater Fields for Laravel Nova
MIT License
778 stars 229 forks source link

Can't update models with any flexible field #515

Open Jigsaw5279 opened 2 months ago

Jigsaw5279 commented 2 months ago

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

MohammadAbusaleh commented 2 months ago

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

Jigsaw5279 commented 2 months ago

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

tursunbaevz commented 2 months ago

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

Frozenpath commented 2 months ago

I have the same problem.

ryzr commented 2 months ago

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)];
}
mabdullahsari commented 1 month ago

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.

DylanS94 commented 1 month ago

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.