How can I display files from a related entity on my show operation? It uses "upload_multiple" #1246
Replies: 3 comments 1 reply
-
To display files from a related entity on your show operation using the "upload_multiple" field, you'll need to make some adjustments to your approach. Based on the information provided in the knowledge sources, here's a solution that should work for your case:
Here's how you can implement this:
CRUD::addColumn([
'name' => 'processed_job_files',
'label' => 'Processed Job Files',
'type' => 'custom_upload_multiple',
'value' => function ($entry) {
return $entry->processedJobs->pluck('engineer_images')->flatten()->toArray();
},
'disk' => 'public', // adjust this if you're using a different disk
'visibleInTable' => false,
'visibleInModal' => false,
'visibleInExport' => false,
'visibleInShow' => true,
]);
@php
$value = data_get($entry, $column['name']);
$column['disk'] = $column['disk'] ?? null;
$column['prefix'] = $column['prefix'] ?? '';
$column['suffix'] = $column['suffix'] ?? '';
$column['wrapper']['element'] = $column['wrapper']['element'] ?? 'a';
$column['wrapper']['href'] = $column['wrapper']['href'] ?? function($file_path, $disk) { return Storage::disk($disk)->url($file_path); };
$column['wrapper']['target'] = $column['wrapper']['target'] ?? '_blank';
@endphp
<span>
@if ($value && count($value))
@foreach($value as $file_path)
@php
$href = $column['wrapper']['href']($file_path, $column['disk']);
@endphp
<{{$column['wrapper']['element']}}
href="{{ $href }}"
target="{{ $column['wrapper']['target'] }}"
>
{{ $column['prefix'] }}
{{ Str::limit(basename($file_path), 40) }}
{{ $column['suffix'] }}
</{{$column['wrapper']['element']}}>
@if (!$loop->last), @endif
@endforeach
@else
-
@endif
</span> This approach uses a custom column type to display the files from the related Note that this solution assumes that your Remember to ensure that your file paths are correctly stored and that your storage disk is properly configured. This solution should allow you to display the files from your related ℹ️ Please note our AI Bot does not reply to follow-ups, this will be the only AI answer in this thread. If the AI helped you find the answer, please mark it as answered or write details below so it can learn. Then close the conversation. If not, please give more information for when a human reads this. ℹ️ Relevant sources: Is there a way to use upload multiple for a 1-n relationship? Please help me |
Beta Was this translation helpful? Give feedback.
-
Hey @NuktukDev It should probably be done through the CRUD::column('processedJobs')->subfields([
[
'name' => 'engineer_images',
'type' => 'upload_multiple',
'withMedia' => true,
],
]); Haven't tested it tho, there were some issues regarding relationships in uploaders that we are fixing in v7 (crud) v2 (media-library). Another possible solution is creating a custom column, and add it to your crud like: CRUD::column('processedJobs.engineer_images')->type('my_custom_column');
// in your `my_custom_column` you should be able to have access to the `$column['value']` Cheers |
Beta Was this translation helpful? Give feedback.
-
Due to not activity, I will close this issue, but please feel free to re-open or create a new one if needed. Cheers. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Important Info: I use spatie media library
I have two entities that are related to each other. Job and ProcessedJob.
On the "Job" show operation, I want to show a list of of related ProcessedJob's and their attributes, as well as the Job entity attributes underneath.
I'm currently doing this manually with a widget and a loop, but I can't get the ProcessedJob's "upload_multiple" column to display the files.
I think it's because the entity is set on "Job". How can I do this?
Here's my hacky way so far:
Note: The "engineer_images" is not a column in the DB.
Beta Was this translation helpful? Give feedback.
All reactions