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