setupShowOperation - print all fields from many to many relationship #792
-
Dear Laravel Backpack community. I'm really struggling with the setupShowOperation. All others CRUD are pretty nice, but I'm lacking a lot of functionalities for that specific part of the project. I've got a model I'm printing on the setupShowOperation, this model is linked to several other models with hasMany relationships. I found a lot of different fields to print one field from the relationship model, but I would like for example to print all the fields of the relationship with the repeatable column. Is this doable ? I can't manage to do that. Another question, I would also like to print list of elements (datatables for example) on the setupShowOperation to print all related relationships of a specific model. I've found this package : https://github.com/izica/relations-widgets-for-backpack but sounds like it's not working with V6, and I don't want it to work with a widget, but in a tab of the show CRUD operation. Is there any way to achieve this ? I can easily print one field from the hasMany relationship : But as soon as I want to print more fields, nothing is showing : ![]() |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 1 reply
-
Hello @Astriel First we do not maintain the https://github.com/izica/relations-widgets-for-backpack package. If you encounter issues with it, please contact the package author for support. Now we dont have a field to show realations as you need, thats need to be a customize solution, you can start with something like:
Cheers. |
Beta Was this translation helpful? Give feedback.
-
Hello @jorgetwgroup About the problem I was facing, I've managed to create a custom column thanks to ChatGPT. Here is the result if it can help someone : $this->crud->addColumn([
'name' => 'relationshipName',
'label' => 'My table',
'tab' => '2 - My tab',
'type' => 'custom_table',
'value' => function ($entry) {
// Get the job relationship from the main model
return $entry->myrelation()->get([
'field_1',
'field_2',
'field_3',
])->toArray();
},
'columns' => [
'field_1' => __('backpack.field_1'),
'field_2' => __('backpack.field_2'),
'field_3' => __('backpack.field_3'),
],
'escaped' => true,
'default' => 'No data available',
'visibleInDetail' => true,
]); And here is the custom_table.blade.php : @php
$column['value'] = $column['value'] ?? data_get($entry, $column['name']);
$column['escaped'] = $column['escaped'] ?? true;
if($column['value'] instanceof \Closure) {
$column['value'] = $column['value']($entry);
}
if (is_string($column['value'])) {
$column['value'] = json_decode($column['value'], true);
}
if(is_object($column['value'])) {
$column['value'] = (array)$column['value'];
}
if (is_array($column['value']) && !empty($column['value']) && !is_multidimensional_array($column['value'])) {
$column['value'] = array($column['value']);
}
@endphp
<span>
@if (!empty($column['value']) && count($column['columns']))
<table class="table table-bordered table-responsive table-striped m-b-0">
<thead>
<tr>
@foreach($column['columns'] as $tableColumnKey => $tableColumnLabel)
<th>{{ $tableColumnLabel }}</th>
@endforeach
</tr>
</thead>
<tbody>
@foreach ($column['value'] as $tableRow)
<tr>
@foreach($column['columns'] as $tableColumnKey => $tableColumnLabel)
<td>
@if($column['escaped'])
{{ $tableRow[$tableColumnKey] ?? '' }}
@else
{!! $tableRow[$tableColumnKey] ?? '' !!}
@endif
</td>
@endforeach
</tr>
@endforeach
</tbody>
</table>
@else
{{ $column['default'] ?? '-' }}
@endif
</span> Hope this can help someone else, because I think other people will face the same issue. That's why I think something like the package I've linked should be included in Backpack by default :-) |
Beta Was this translation helpful? Give feedback.
Hello @jorgetwgroup
I've been in touch with someone who created an adapted version of the package. So I'm going to use that.
About the problem I was facing, I've managed to create a custom column thanks to ChatGPT.
Here is the result if it can help someone :