search

Home  >  Q&A  >  body text

Laravel reads multiple fields from the database and displays them in the view. If the field is empty, it will not be displayed. Is it necessary to add an if judgment to each field?

Laravel reads multiple fields from the database and displays them in the view. If the field is empty, it will not be displayed. Is it necessary to add an if judgment to each field? Like this:

@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

Is there any other way to write it?

曾经蜡笔没有小新曾经蜡笔没有小新2752 days ago376

reply all(2)I'll reply

  • phpcn_u1582

    phpcn_u15822017-05-16 16:49:08

    In fact, you are on the backend (the controller can better judge, or give a default value), and you are on the view, so the experience is very bad

    reply
    0
  • PHPz

    PHPz2017-05-16 16:49:08

    Write a Transformer to force data type conversion to string

    reply
    0
  • Cancelreply