laravel / nova-issues

554 stars 34 forks source link

Unable to get KeyValue Field to work. #1602

Closed tanthammar closed 5 years ago

tanthammar commented 5 years ago

Description:

Trying to use KeyValue Field as documented.

Steps To Reproduce:

Existing data stored in meta, MySql type JSON field:

{
    "stripe_id": "xxxxxxxxx",
    "approved_terms": "1",
    "active_subscriber": "1",
    "approved_terms_at": "2018-05-30 06:05:43",
    "created_exhibitor": "1",
    "weekly_billing_id": "xxxxxxxxxxxx",
    "subscription_plan_id": "xxxxxxxx",
    "subscription_plan_name": "xxxxxx",
    "managed_account_plan_id": "xxxxxxxxx"
}

On Model

//tried this
protected $casts = [
   'meta' => 'array',
];
//and this
protected $casts = [
   'meta' => 'json',
];
//and this
protected $casts = [
];

On Nova Resource

use Laravel\Nova\Fields\KeyValue;
KeyValue::make('Meta')->rules('json'),
wdelfuego commented 5 years ago

What error message do you get when trying to save with the KeyValue field present on the Nova resource?

davidhemphill commented 5 years ago

I was not able to reproduce your issue. I have a KeyValue field, with a nullable json column type, cast as array, filled with your example JSON, and the field is working correctly. Make sure you assets are updated using php artisan nova:publish --force. If you find out a way to reproduce this, please open another issue. Thanks!

WhitePolice commented 5 years ago

Throws "Array to string conversion" when used with "Many-to-Many" relationship.

// 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');
}

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

Try

protected $casts = [
  'meta' => 'collection',
];
kumarkishan070997 commented 4 years ago

tried everything but still getting the same error -- array to string conversion

but i tried the same thing on another resource file and it's working fine .

yazeed commented 2 years ago

Same here, tried collection, array, and AsArrayObject::class casting on the pivot model, still won't work. Latest Nova version, and Laravel 9+, trying to cast to a json field on a postgres DB.