LoginSignup
34
21

More than 1 year has passed since last update.

【Laravel】Bladeで@ifではなく三項演算子を使う

Last updated at Posted at 2019-06-08

はじめに

LaravelのBladeでは、@ifを使ってif文の分岐ができますが、{{}}内で三項演算子を使うこともできます。

好みにもよりますが、三項演算子を使った方がBladeがすっきりする場合もあるかと思います。

使用例その1

<div>
  @if ($item->is_approved)
    承認済
  @else
    未承認
  @endif
</div>

:point_down_tone2:

<div>
  {{ $item->is_approved ? '承認済' : '未承認' }}
</div>

使用例その2

<div>
  @if ($item->notice)
    {{ $item->notice }}
  @else
    お知らせはありません
  @endif
</div>

:point_down_tone2:

<div>
  {{ $item->notice ?: 'お知らせはありません' }}
</div>
34
21
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
34
21