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

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

Open
delennerd opened this issue Mar 1, 2024 · 0 comments

Comments

@delennerd
Copy link

  • 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
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

1 participant