Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can't update models with any flexible field #515

Open
Jigsaw5279 opened this issue Apr 12, 2024 · 8 comments
Open

Can't update models with any flexible field #515

Jigsaw5279 opened this issue Apr 12, 2024 · 8 comments

Comments

@Jigsaw5279
Copy link

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
Copy link

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

@Jigsaw5279
Copy link
Author

Jigsaw5279 commented Apr 14, 2024

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
Copy link

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

@Frozenpath
Copy link

I have the same problem.

@ryzr
Copy link

ryzr commented Apr 18, 2024

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
Copy link
Contributor

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
Copy link

DylanS94 commented Jun 3, 2024

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.

@AvishkaSen
Copy link

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

8 participants