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

with('relation') . cause utf8 encode error #68

Closed
mohamedsharaf opened this issue Sep 13, 2018 · 5 comments
Closed

with('relation') . cause utf8 encode error #68

mohamedsharaf opened this issue Sep 13, 2018 · 5 comments

Comments

@mohamedsharaf
Copy link

mohamedsharaf commented Sep 13, 2018

the problem is that laravel toArray add pivot data and if it include uuid it will make utf8 error

i fixed this by removing pivot from the array
if (isset($array['pivot'])){
unset($array['pivot']);
}

this is the new trait toArray


    public function toArray()
    {
        $uuidAttributes = $this->getUuidAttributes();
        $array = parent::toArray();
        if (! $this->exists || ! is_array($uuidAttributes)) {
            return $array;
        }
        foreach ($uuidAttributes as $attributeKey) {
            if (! array_key_exists($attributeKey, $array)) {
                continue;
            }
            $uuidKey = $this->getRelatedBinaryKeyName($attributeKey);
            $array[$attributeKey] = $this->{$uuidKey};
        }

  if (isset($array['pivot'])){
            unset($array['pivot']);
        }


        return $array;
    }

     Schema::create('roles', function (Blueprint $table) {
            $table->uuid('id');
            $table->string('name')->unique();
        });

        Schema::create('permissions', function (Blueprint $table) {
            $table->uuid('id')->primary();
            $table->string('name');
        });
    Schema::create('permission_role', function (Blueprint $table) {
            $table->uuid('permission_id')->index()->foreign('permission_id')->references('slug')->on('permissions')->onDelete('cascade');;
            $table->uuid('role_id')->index()->foreign('role_id')->references('id')->on('roles')->onDelete('cascade');
        });

class Role extends Model
{
    use HasBinaryUuid;

    protected $uuids = [
        'id'
    ];
  public function permissions()
    {
        return $this->belongsToMany(Permission::class,'permission_role','role_id','permission_id');

    }

}
class Permission extends Model
{
    use HasBinaryUuid;

    protected $uuids = [
        'id'
    ];

 public function roles()
    {
        return $this->belongsToMany(Role::class,'permission_role','permission_id','role_id');
    }

}



    Route::any('roles', function () {
        return Role::with('permissions')->get() ;
    });


Malformed UTF-8 characters, possibly incorrectly encoded
i think the model to Array not working on collections and join

any help

@isocroft
Copy link
Contributor

I think... toArray() works just fine in the trait. However, i need to dig deeper into the cause of this.

In your case, what does $array['pivot'] contain exactly ?

@mohamedsharaf
Copy link
Author

pivot: {
role_id: Binarydata,
permission_id: Binarydata
}

@isocroft
Copy link
Contributor

isocroft commented Oct 4, 2018

No need to modify the code in the manner you're suggesting. Simply add the '[pivot]' to your $hidden property array on your model like so:

<?php

class Role extends Model
{

    protected $hidden = array('pivot');

}

See: laravel/framework#745 for more info

@isocroft
Copy link
Contributor

isocroft commented Oct 4, 2018

I think this issue can be closed out (for now). It would be reopened if there's any further concerns

@freekmurze
Copy link
Member

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