1

I know that for a normal variable I can use something like {{ $vid->shares ? : '0' }} to show default information in Laravel 5.7 blade files.

Now I need to show image source, which I am doing like <img src="{{ url($vid->thumb_file) }}"> this works fine until I hit a empty value.

is there anything to avoid such case by adding a default fallback image URL? Or "if else" is only a better solution?

Something like {{ url($vid->thumb_file) ?: url('/images/video-thumbnail.png') }} which is not working.

2
  • 2
    Check it like: $vid->thumb_file ? url($vid->thumb_file) : url('/images/video-thumbnail.png') Commented Nov 27, 2018 at 6:23
  • 1
    Yes, that works. <img src="{{ $vid->thumb_file ? url($vid->thumb_file) : url('/images/video-thumbnail.png') }}" alt="" class="img-fluid"> Commented Nov 27, 2018 at 6:28

2 Answers 2

3

You can use like below:

<img src="{{ $vid->thumb_file ? url($vid->thumb_file) : url('/images/video-thumbnail.png') }}" alt="" class="img-fluid">

This is as same as Ternary Operator you used above: {{ $vid->shares ?: '0' }}

You can also check it in your controller, like:

if (file_exists(public_path() . '/images/' . $vid->image)) {
    $img = $vid->image;
} else {
    $img = '/images/video-thumbnail.png';
} 

Hope this helps!

Sign up to request clarification or add additional context in comments.

Comments

2

You can use this way:

{{ isset($vid->thumb_file) ? $vid->thumb_file : url('/images/video-thumbnail.png') }}

<img src="{{ isset($vid->thumb_file) ? $vid->thumb_file : url('/images/video-thumbnail.png') }}" alt="" class="img-fluid">

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.