Skip to content
This repository has been archived by the owner on Dec 10, 2018. It is now read-only.

json_encode results in JSON_ERROR_UTF8 because of uuid field #13

Closed
faustbrian opened this issue Nov 30, 2017 · 3 comments
Closed

json_encode results in JSON_ERROR_UTF8 because of uuid field #13

faustbrian opened this issue Nov 30, 2017 · 3 comments

Comments

@faustbrian
Copy link
Contributor

faustbrian commented Nov 30, 2017

Currently it is not possible to json serialize any model that uses this package because the uuid field is in binary format.

json_encode(\App\Announcement::first());

dd(json_last_error()); // Results in 5 which equals JSON_ERROR_UTF8

I am currently solving this issue by using protected $hidden = ['uuid']; and protected $appends = ['id'] where ID is the method from here #12

It might be worth considering to replace the uuid field data with the uuid_text attribute value for the array format to allow working with it without any extra hacks like I do right now.

@faustbrian
Copy link
Contributor Author

This is what I currently use as a workaround for all my models.

<?php

namespace App\Traits;

use Illuminate\Database\Eloquent\Model;

trait HasUuidPrimaryKey
{
    /**
     * Get the textual id of the entity.
     *
     * @return string
     */
    public function getIdAttribute()
    {
        return $this->uuid_text;
    }

    /**
     * Get the hidden attributes for the model.
     *
     * @return array
     */
    public function getHidden()
    {
        return ['uuid'] + $this->hidden;
    }

    /**
     * Get the primary key for the model.
     *
     * @return string
     */
    public function getKeyName()
    {
        return 'uuid';
    }

    /**
     * Get the value indicating whether the IDs are incrementing.
     *
     * @return bool
     */
    public function getIncrementing()
    {
        return false;
    }
}

@freekmurze
Copy link
Member

@brendt didn't we take care of this while developing the package?

@brendt
Copy link
Contributor

brendt commented Nov 30, 2017

This packages takes care of part of this problem. Namely when a model is serialised in queues.

I'll answer on the concrete solution in PR #12

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

No branches or pull requests

3 participants