Bird
0
0

You want to display a default message if a collection $posts is empty using Blade directives. Which snippet correctly does this?

hard📝 Application Q8 of 15
Laravel - Views and Blade Templates
You want to display a default message if a collection $posts is empty using Blade directives. Which snippet correctly does this?
A@forelse($posts as $post) {{ $post->title }} @empty <p>No posts found.</p> @endforelse
B@foreach($posts as $post) {{ $post->title }} @else <p>No posts found.</p> @endforeach
C@if(empty($posts)) <p>No posts found.</p> @else @foreach($posts as $post) {{ $post->title }} @endforeach @endif
D@loop($posts as $post) {{ $post->title }} @empty <p>No posts found.</p> @endloop
Step-by-Step Solution
Solution:
  1. Step 1: Recall Blade directive for looping with empty check

    @forelse loops over items and provides @empty for no items.
  2. Step 2: Match correct syntax

    @forelse with @empty and @endforelse is correct; others use invalid directives or syntax.
  3. Final Answer:

    @forelse with @empty block -> Option A
  4. Quick Check:

    Use @forelse for loop with empty fallback [OK]
Quick Trick: Use @forelse ... @empty ... @endforelse for empty collection handling [OK]
Common Mistakes:
  • Using @else inside @foreach which is invalid
  • Using non-existent @loop directive
  • Not handling empty collections properly

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Laravel Quizzes