31
28

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 5 years have passed since last update.

laravel の Blade Template でループをするときは @forelse でスッキリ

Last updated at Posted at 2017-01-01

Bladeでループを書くときは、こんな風に入れ子で書いていました。

*.blade.php
@if ($task->count())
    @foreach ($tasks as $task)
        <li>{{ $task->name }}</li>
    @foreach
@else
    no task
@endif

もちろんこれでいいのですが、 @forelse を使うとスッキリ書くことができます。

*.blade.php
@forelse ($tasks as $task)
    <li>{{ $task->name }}</li>
@empty
    no tasks
@endforelse

マニュアルを見ると、この構文は5.1以降で使えるようです。

31
28
2

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
31
28

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?