Skip to content
Discussion options

You must be logged in to vote

Hi there ✋🏻 .

TLDR:
Use @forelse if you want an elegant way to handle empty states using Blade.
Use @foreach for regular loop statements.


I could be misunderstanding the use of the @forelse directive

Kind of...

The idea of the @foresle is to add an elegant way to handle empty states using Blade.

e.g. This displays a message when the posts collections is empty

@forelse ($posts as $post)
    {{ $post->title }}
@empty
    ¡There are not posts!
@endforelse

If you don't want to display any message when the when the posts collections is empty, you can use the regular @foreach blade statement.
e.g.

@foreach ($posts as $post)
    {{ $post->title }}
@endforeach

Replies: 1 comment 2 replies

Comment options

You must be logged in to vote
2 replies
@ryangurn
Comment options

@ryangurn
Comment options

Answer selected by ryangurn
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Ideas
Labels
None yet
2 participants