问题
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?
回答1:
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.
回答2:
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')
回答3:
Try this. It's the only mention of "JSON" in nova fields description.
回答4:
For example the value for 'desc' column is something like :
"{attributes : {'desc' : 'test', 'width' : '500'}}"
You can directly put it in table format :
KeyValue::make('Desc', 'desc**.attributes**')
Hope this helps
来源:https://stackoverflow.com/questions/52386631/laravel-nova-map-database-json-column-to-field