key-value and json arrays: {closure}(): Argument #1 ($value) must be of type ?string, array given

Hey all,
I'm working on an admin section for my portfolio page.
The data is from a bunch of Markdown files with some frontmatter; this frontmatter has been converted and stored as json in a sqlite database. When editing the Project, I chose to display the frontmatter with a KeyValue form component. After trying to save an instance without changing the values (meaning they should be valid), I get the following error:
Filament\Forms\Components\KeyValue::Filament\Forms\Components\{closure}(): Argument #1 ($value) must be of type ?string, array given

Could it be that an array value is incompatible with the KeyValue component? Or am I doing something wrong ?
Solution
Looking at it, you are storing as an array but we load that magically and it renders ok. But it should always be stored as a string in a keyValue.... you could look to cast it to a string with attributes.

If you are insistent on storing it as an Array you can use this model class for your project using the get/set attributes

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class Project extends Model
{
    use HasFactory;

    protected $fillable = ["fm", "content"];
    protected $casts = [
        "fm" => "array"
    ];


    public function getFmAttribute($value)
    {
        $fm = json_decode($value, true);

        if($tags = data_get($fm, 'tags')) {
            $fm['tags'] = implode(',', $tags);
        }

        return $fm;
    }

    public function setFmAttribute($value)
    {
        if($tags = data_get($value, 'tags')) {
            $value['tags'] = explode(',', $tags);
        }
        // dd($value);
        return $value;
    }
}
Was this page helpful?
key-value and json arrays: {closure}(): Argument #1 ($value) must be of type ?string, array given - Filament