I want to show the name field from address json (using algolia), how do i do that?
I tried this:
$this->crud->addColumn([ 'name' => "address", 'label' => 'Address', 'type' => 'array', ]);
But can’t manage to work. Also, it always return error.
In preview, i want to show full address too, but only got json return.
Thanks in advance
Advertisement
Answer
Managed to work with Custom Fields.
Controller:
$this->crud->addColumn([ 'name' => 'address', //db field 'label' => "Address", 'type' => 'customAddress' //name of custom created custom field ]);
Create a custom field at: resourcesviewsvendorbackpackcrudcolumns I named it as ‘customAddress.blade.php’
Custom Field:
{{-- customAddress--}} <?php $object = (object) json_decode($entry['address'], true); ?> <span><?php echo $object->value; ?></span>
Also you can display with foreach, something like $object->name, $object->country, etc…
Hope this helps someone.
Edit 1 – Backpack 4.1 updated column
For everyone who are migrating from Backpack 4.0 to 4.1 you should update this custom column too.
Here is an updated code for Backpack v4.1. In v4.1 there is a difference if column is escaped or not.
I also added a check in case of a JSON parsing error.
{{-- customAddress--}} @php $value = data_get($entry, $column['name']); $column['escaped'] = $column['escaped'] ?? false; json_decode($entry['address']); if(json_last_error() == JSON_ERROR_NONE){ $object = (object) json_decode($entry['address'], true); $value = $object->value; } else{ $value = $entry['address']; } @endphp <span> <span class="d-inline-flex"> @includeWhen(!empty($column['wrapper']), 'crud::columns.inc.wrapper_start') @if($column['escaped']) {{ $value }} @else {!! $value !!} @endif @includeWhen(!empty($column['wrapper']), 'crud::columns.inc.wrapper_end') </span> </span>
Hope this helps someone who is migrating from Backpack 4.0.