Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/241.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 刀片服务器阵列中img缺失时的Laravel create if语句_Php_Laravel - Fatal编程技术网

Php 刀片服务器阵列中img缺失时的Laravel create if语句

Php 刀片服务器阵列中img缺失时的Laravel create if语句,php,laravel,Php,Laravel,当我的电子邮件在数组中缺少图像时,如何在laravelblade中创建if语句? 当数组中未定义电子邮件时,laravel显示错误 我的目标是在数组中定义带有图像的电子邮件时显示图像,如果未定义电子邮件,则显示自定义jpg 我的阵列 $picture = array ( 'email1@email.com' => '1.jpg', 'email2@email.com' => '2.jpg', 'email3@email.com' => '3,

当我的电子邮件在数组中缺少图像时,如何在laravel
blade
中创建
if
语句? 当数组中未定义电子邮件时,laravel显示错误

我的目标是在数组中定义带有图像的电子邮件时显示图像,如果未定义电子邮件,则显示自定义jpg

我的阵列

$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"/>
user()

您可以使用null coalesce操作符检查是否设置了该数组键并使用它或使用默认值:

$picture[Auth()->user()->mail_from] ?? 'custom.jpg'
您可以使用
Collection@get
还有:

collect($picture)->get(Auth()->user()->mail_from, 'custom.jpg');

在数组中使用未定义的电子邮件进行尝试,并得到以下错误:
undefined index:email|Test@email.com
我使用了您的第一个代码:{{asset('/images/'.$picture[Auth()->user()->mail_from]??'images/custom.jpg')}
get(Auth()->user()->mail_from,'custom.jpg'))}height=“333”width=“222”alt=“”style=“margin left:65px”/>
有效!