Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel Nova - Map database json column to field

I have a database model with json column type called attributes. This column will contain additional details of the model.

For example: attributes : {'desc' : 'test', 'width' : '500'}.

I would like to display individual attribute (desc & width) in attributes column as a html field in Laravel admin resource.

I checked supported field types and doesn't seem supports.

Any suggestions how I can do this?

like image 251
Saumini Navaratnam Avatar asked Oct 15 '25 03:10

Saumini Navaratnam


2 Answers

Here's how I was able to accomplish this in Nova 2.0. In my case I have a JSON column called fields. In the model, fields is cast to an array.

protected $casts = [
    'fields' => 'array'
];

Then in the resource, the column is mapped using the -> operator.

Text::make('Name', 'fields->name')
like image 72
Brad Goliber Avatar answered Oct 17 '25 19:10

Brad Goliber


I'm not sure if it's the recommended answer but i've found a way that should work for you.

Use an Eloquent Mutator: Eloquent Mutators

Then you define a getDescAttribute and a setDescAttribute and pass it in nova like a property on the model like so:

Text::make('Desc', 'desc')

Hope this helps.

like image 24
SirBennyLavaThe3rd Avatar answered Oct 17 '25 18:10

SirBennyLavaThe3rd



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!