35
21

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have 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>
35
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
35
21

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?