corcel / corcel

Use WordPress backend with Laravel or any PHP application

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Getting error unserialize(): Error at offset 0 of 2 bytes - using Carbon Fields

delennerd opened this issue · comments

  • Corcel Version: 7.0.0
  • Framework Name & Version: WordPress 6.4.3
  • PHP Version: 8.1
  • Database Driver & Version: MariaDB
  • Carbon Fields: 3.6

Description:

I use Carbon Fields to create my meta fields in custom posts and users.
When I create a new post or user (use my custom models) and return the data in my ajax response then I get errors:

unserialize(): Error at offset 0 of 2 bytes in /MY_PROJECT_DIR/vendor/jgrossi/corcel/src/Model/Meta/Meta.php on line 38

I also test it with these command in my site:

$testUser = \App\Models\User::find(42);
var_dump($testUser->toJSON());

But when I save my field values in my code and serialize them, I don't get an error. But in my backend I see the serialized values and I don't want this.
When I display the value with unserialize then the value is ok.

I think the problem is because your function:

public function getValueAttribute()
    {
        try {
            $value = unserialize($this->meta_value);

            return $value === false && $this->meta_value !== false ?
                $this->meta_value :
                $value;
        } catch (Exception $ex) {
            return $this->meta_value;
        }
    }

What can I do?

Steps To Reproduce:

  1. Create a custom post meta field with Carbon Fields 3.6
  2. Go to a post with your new meta field and save some text in it
  3. On your site make Post::find(YOUR_POST_ID)->toJSON()
    You can also create your own model and extend Post