Skip to content

[Question]:Array to string conversion error when using EditorjsTextField – how should model and migration be defined? #17

@HosseinEsteki

Description

@HosseinEsteki

What happened?

I’m using the EditorjsTextField component in my Filament form.
When I try to save an article, I get the following error:

Array to string conversion (Connection: mysql, Host: 127.0.0.1, Port: 3306, Database: laravel, SQL: insert into `blog_articles` ...)

It seems that the field returns a JSON string, but my model/migration setup might be incorrect.
I’m not sure what the recommended database column type and model cast should be for EditorjsTextField.

How to reproduce the bug

How to reproduce the bug

  1. Create a migration with a json column for content:
Schema::create('blog_articles', function (Blueprint $table) {
    $table->id();
    $table->string('title');
    $table->json('content'); // or longText?
    $table->json('content_images')->nullable();
    $table->timestamps();
});
  1. Define the model:
class Article extends Model implements HasMedia
{
use ModelHasEditorJsComponent,InteractsWithMedia;
    protected $table = 'blog_articles';

    protected $fillable = ['title', 'description', 'keywords', 'content', 'status', 'user_id'];

    protected $casts = [
        'content' => 'array', // or string?
    ];
}
  1. Use EditorjsTextField in the Filament form:
EditorjsTextField::make('content')
    ->label('Content')
    ->required()
    ->columnSpanFull();
  1. Try to create a new article with some content.

Expected behavior

I would like to know the correct way to define the database column type and the model cast for EditorjsTextField.
Should the column be json, text, or longText?
Should the cast be string or array?

Could you please provide an example of a recommended model + migration setup for this field?


Thanks a lot for your help and for maintaining this package!


### Package Version

2.3

### PHP Version

8.4

### Laravel Version

12

### Which operating systems does with happen with?

Windows

### Notes

_No response_

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions