lewis4u
lewis4u

Reputation: 15047

Laravel blade - show fa-icon dependable on file type

I have this foreach loop to list out all the files

@foreach ($event->event_files as $f => $file)
  <div class="col-sm-2">
    <a href="{{\Storage::disk('public')->url($file->path)}}">{{$file->name}}</a>
  </div>
@endforeach

And what i want to make is a preview icon for every file so that it looks like this:

enter image description here

I have the icons and i have the file type extensions....i just don't know how to connect all of this in a foreach loop.

Any ideas?

Upvotes: 1

Views: 2047

Answers (1)

LF-DevJourney
LF-DevJourney

Reputation: 28529

make array in controller:

$icons = [
        'pdf' => 'pdf',
        'doc' => 'word',
        'docx' => 'word',
        'xls' => 'excel',
        'xlsx' => 'excel',
        'ppt' => 'powerpoint',
        'pptx' => 'powerpoint',
        'txt' => 'text',
        'png' => 'image',
        'jpg' => 'image',
        'jpeg' => 'image',
    ];

and send it to view with

return view('edit', compact('icons'));

and in blade

@foreach ($event->event_files as $f => $file)
  <div class="col-sm-2 text-center">
     <i class="fa fa-file-{{$icons[$file->type]}}-o fa-5x text-center"/></i>

     <div class="clearfix"></div>

     <a href="{{\Storage::disk('public')->url($file->path)}}">{{$file->name}}</a>
     </div>
@endforeach

and you will get something like this:

enter image description here

Upvotes: 3

Related Questions