How do I check for an image name in the database and return a static image if NULL in a Laravel Blade Template?

Viewed 304

How do I use the Null coalescing operator to check for an image name in the database, otherwise return a static image? The following is a line from my Laravel Blade file that I am trying to work with.

<img src="{{ URL::asset('/images/user/'. $profile->photo ?? 'Firefighter-Silhouette.png') }}" class="rounded-circle shadow-2 img-thumbnail" alt="">

Not only does it not return the static image, it somehow removes the final forward slash in the '/images/user/' string.

<img src="https://siriusfireweb.local/images/user" class="rounded-circle shadow-2 img-thumbnail" alt="">

TIA

1 Answers

1st condition

 <img src="{{asset('/images/user/'. @if($profile->photo != null)
    {{ $profile->photo}}
    @else
    {{'Firefighter-Silhouette.png'}}
    @endif }}" class="rounded-circle shadow-2 img-thumbnail" alt="">
  • you can check this code its help you to find the null return value.

2nd condition

@php 
if($profile->photo != null){ // you can check your condition here.
$img = asset('/images/user/'.$profile->photo);
}else{
$img = asset('/images/user/Firefighter-Silhouette.png');
}

@endphp

now you can use $img variable on img src

Related