laravel / nova-issues

554 stars 34 forks source link

KeyValue throws "Array to string conversion" when used with "Many-to-Many" relationship #1738

Closed WhitePolice closed 5 years ago

WhitePolice commented 5 years ago

Description:

When using KeyValue field on BelongsToMany I'm getting an error "Array to string conversion".

Steps To Reproduce:

  1. Create models User and Role
  2. Add relation for the both models
  3. Add meta column with json nullable on pivot table
  4. Add BelongsToMany with KeyValue on resource
// Roles
protected $casts = [
  'meta' => 'array',
];

public function users()
{
  return $this->belongsToMany(User::class)->withPivot('meta');
}

// Users
protected $casts = [
  'meta' => 'array',
];

public function roles()
{
  return $this->belongsToMany(Role::class)->withPivot('meta');
}

// RoleUser
protected $casts = [
  'meta' => 'array',
];

// Nova User Resource
BelongsToMany::make('Roles')
  ->fields(function () {
    return [
      KeyValue::make('Meta')->rules('json'),
    ]
  });
dillingham commented 5 years ago

To type cast on pivot tables you need to make a pivot model

see Defining Custom Intermediate Table Models here

WhitePolice commented 5 years ago

@dillingham tried creating intermediate model and adding $casts attribute in it but still throwing "Array to string conversion" error.

dillingham commented 5 years ago

@WhitePolice update the issue with the new code then

jbrooksuk commented 5 years ago

Sorry, but there's not enough information to go off of here. If the issue you have still persists, please feel free to open a new issue with all the details of what you're trying, what you expect to happen, what's happening, and how to reproduce it. Thanks!

yazeed commented 2 years ago

I'm facing the same issue and I have the casts properly on the pivot model.

i'm doing an edit on that intermediate model (polymorphic) and getting array to string conversion error.