Skip to content
Advertisement

Laravel create if statement when img missing in array in blade

How to create if statement in laravel blade when my email with image missing in array ? When email is not defined in array, laravel show error.

My goal is when email with image are defined in array show the image, if email isnt defined show custom jpg.

my array

$picture = array (
      'email1@email.com' => '1.jpg',
      'email2@email.com' => '2.jpg',
      'email3@email.com' => '3,jpg'
);

blade

<img src="{{ asset('/images/'.$picture[(Auth()->user()->mail_from)] ) }}" height="333" width="222" alt="" style="margin-left: 65px"/>

Advertisement

Answer

You could use the null coalesce operator to check if that array key is set and use it or use a default:

$picture[Auth()->user()->mail_from] ?? 'custom.jpg'

You could use Collection@get as well:

collect($picture)->get(Auth()->user()->mail_from, 'custom.jpg');
User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement