rohan_mhz's avatar

Display specific images with captions

I want to display only those images in the carousel which have captions and not those which have no captions.

I was thinking of using if function and check if the incoming image has the caption or not, but I don't know how to do it. is there any other way of doing it?

plz, help me cuz I am new to this.

0 likes
10 replies
kylemilloy's avatar

What's your data look like? Can you post some code?

rohan_mhz's avatar

right now i am displaying all the incoming images and only some have captions.

 @foreach ($packages as $key => $package)
                
                <div class="carousel-item{{ $key == 0 ? ' active' : '' }}">
              
                    <img class="d-block w-100" src="{{ Storage::disk('local')->url('img/package/' . $package->image) }}">
                    <div class="carousel-caption d-none d-md-block">
                        <div class="container">
                            {!! $package->caption !!}
                            <a class="button-theme" href="{{ route('travel.itinerary', $package->slug) }}">learn more</a>
                        </div>
                    </div>
                </div>
                @endforeach

index.blade.php file

munazzil's avatar

Use like this and check your controller function,

$packages = Packages::get()->toArray();
 
  {{ $package->caption }}

and you can get images like this as well,

          <img class="d-block w-100" src="{{ asset('img/package/' . $package->image) }}">
rohan_mhz's avatar

@munazzil ok, but I need to get only those images which have captions. Currently, I have stored captions for only 4 images and I have 8 total images. how do I get those 4 images with the captions??

munazzil's avatar

Something like below you can try,

 {{ $package->image->caption }}

else you can get sort data from controller,

  $packages = Packages::pluck('images')->sortBy('caption')->get();
Snapey's avatar
Snapey
Best Answer
Level 122

here is a tip for this forum

ignore anything @munazzil tells you

     @foreach ($packages as $key => $package)
            @if( $package->caption)
                <div class="carousel-item{{ $key == 0 ? ' active' : '' }}">
              
                    <img class="d-block w-100" src="{{ Storage::disk('local')->url('img/package/' . $package->image) }}">
                    <div class="carousel-caption d-none d-md-block">
                        <div class="container">
                            {!! $package->caption !!}
                            <a class="button-theme" href="{{ route('travel.itinerary', $package->slug) }}">learn more</a>
                        </div>
                    </div>
                </div>
          @endif
       @endforeach

and never use {!! !!} unless you REALLY know what you are doing and have previously sanitized the data

2 likes
rohan_mhz's avatar

@snapey ok, I am using TinyMce to record the data so I had to use {!! !!} to get the exact format that I needed to display it in the page.

Snapey's avatar

So did this work for you?

Regarding the {!! !!} - what happens if a user inserts javascript into the caption?

rohan_mhz's avatar

@snapey Sorry for the late reply, Ya that worked perfectly thanks for the help. Now I feel stupid that I couldn't think of such a simple solution.

Snapey's avatar

no problems for the delay. Please mark it solved

Please or to participate in this conversation.