Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
481 views
in Technique[技术] by (71.8m points)

laravel从数据库中读取多个字段在视图中显示,如果字段为空则不显示,每个字段都加一个if判断吗?

laravel从数据库中读取多个字段在视图中显示,如果字段为空则不显示,每个字段都加一个if判断吗?像下面这样:

@if ($user->name!=null)
<div>姓名:$user->name</div>
@endif

@if ($user->email!=null)
<div>邮箱:$user->email</div>
@endif

@if ($user->phone!=null)
<div>电话:$user->phone</div>
@endif

有没有其他写法?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

其实这个你在后端(控制器判断更好,或者给一个默认值),你在视图上,体验很不好


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...