Mukhlis Raza
Mukhlis Raza

Reputation: 220

Laravel: Can't Display Image In Blade File

I tried to display the image in the blade. but it cant load the image, it displays the image of else statement

     <?php $user_image_path = 'images/user_images/' . $userDetails['image']; ?>
   
     @if(!empty($userDetails['image']) && file_exists($user_image_path))
          <img src=" {{ asset('images/user_images/'.$userDetails['image'])}}" alt="Cinque Terre">
     @else
          <img src="{{ asset('images/product_images/small/no_image.png') }}" alt="Cinque Terre">
     @endif

I also debug the code above the if statement it fetches the image

 <?php $product_image_path = 'images/user_images/' . $userDetails['image']; ?>
   <?php
   echo "<pre>";
   print_r($user_image_path);
   die;
   ?>

Upvotes: 1

Views: 1816

Answers (5)

Muhammad Atif Akram
Muhammad Atif Akram

Reputation: 1315

@if(!empty($userDetails['image']))
    <img src="{{ asset('images/user_images/'.$userDetails['image'])}}" alt="Cinque Terre">
  • View the src of image inside inspector. Most of the times it gives us the wrong path instead of public directory.
  • Secondly, there is space in src. Try to remove and run that.
  • If asset function doesn't give the public directory path, add ASSETS_URL In .env to /public

Upvotes: 0

Hedayatullah Sarwary
Hedayatullah Sarwary

Reputation: 2834

Use this code:

<img src=" {{ !empty(asset('images/user_images/'.$userDetails['image'])) ? <img src="{{ asset('images/user_images/'.$userDetails['image'])}}" alt="Cinque Terre"> : <img src="{{ asset('images/product_images/small/no_image.png') }}" alt="Cinque Terre"> }}" alt="Cinque Terre">

Upvotes: 0

Ahmad Karimi
Ahmad Karimi

Reputation: 1373

It is the second condition of your if condition that doesn't match. The function of file_exists can't locate the the image file. You either have to provide the full path for the file_exists , or remove it:

@if(!empty($userDetails['image']))
      <img src="{{ asset('images/user_images/'.$userDetails['image'])}}" alt="Cinque Terre">
@else
      <img src="{{ asset('images/product_images/small/no_image.png') }}" alt="Cinque Terre">
@endif

Upvotes: 0

Donald Kagunila
Donald Kagunila

Reputation: 349

From above comments on your question, seems like you are loading the image with no extension. add the image extension

 <img src="{{  asset('images/user_images/'.$userDetails['image'].".png") }}" alt="">

or for .jpg

<img src="{{  asset('images/user_images/'.$userDetails['image']).".jpg" }}" alt="">

Upvotes: 1

Rashed Rahat
Rashed Rahat

Reputation: 2475

Try:

<img src="{{ !empty($userDetails['image']) ? asset('images/user_images/'.$userDetails['image']) : asset('images/product_images/small/no_image.png') }}" alt="Cinque Terre">

Upvotes: 0

Related Questions