laravel从数据库中读取多个字段在视图中显示,如果字段为空则不显示,每个字段都加一个if判断吗?像下面这样:
@if ($user->name!=null)
<p>姓名:$user->name</p>
@endif
@if ($user->email!=null)
<p>邮箱:$user->email</p>
@endif
@if ($user->phone!=null)
<p>电话:$user->phone</p>
@endif
有没有其他写法?
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号
其实这个你在后端(控制器判断更好,或者给一个默认值),你在视图上,体验很不好
写一个 Transformer 强制数据类型转换成字符串