Learn how to use Blade directives like @if, @foreach, and @forelse to control your view logic and render dynamic lists in your Laravel applications efficiently.
Previously in this course, we explored Implementing Blade Partials: A Guide to DRY Laravel Views to keep our templates clean and reusable. Now that we have the structural basics down, it's time to make our views truly dynamic.
In any real-world application, you rarely display static content. You need to show "active" tasks, hide buttons for unauthenticated users, or render a list of items from a database. Blade makes this easy by providing a clean, readable syntax for common PHP control structures.
At its core, a view is just a representation of state. Sometimes, you only want to show specific parts of that state if certain conditions are met. Instead of writing raw PHP like <?php if ($task->completed): ?>, Blade gives us the @if directive.
The syntax is expressive and mirrors standard PHP, but without the messy curly braces and closing tags.
BLADE@if ($task->is_completed) <span class="badge">Finished</span> @elseif ($task->is_urgent) <span class="badge badge-warning">Urgent</span> @else <span class="badge badge-info">Pending</span> @endif
When building our Task Manager, the most common task you'll perform is iterating over an array or collection of items. Blade provides the @foreach directive for this exact purpose.
If you have an array of tasks passed from your controller, you can loop through them like this:
BLADE<ul> @foreach ($tasks as $task) <li>{{ $task->title }}</li> @endforeach </ul>
One of the most common "gotchas" in development is rendering an empty list. You might show an empty <ul> tag, which looks broken. Laravel provides the @forelse directive to handle this scenario gracefully in one block.
BLADE<ul> @forelse ($tasks as $task) <li>{{ $task->title }}</li> @empty <p>No tasks found. Time to relax!</p> @endforelse </ul>
The @forelse directive checks if the collection is empty. If it is, it automatically jumps to the @empty block. It’s cleaner and more robust than writing an @if check followed by a @foreach.
Let’s apply this to our Task Manager. Open your resources/views/tasks/index.blade.php file. Assume your controller is passing a variable named $tasks to this view.
@forelse loop.@if condition to style the task differently if it's marked as "urgent".@empty directive to display a friendly message when there are no tasks.BLADE<h1>My Tasks</h1> @forelse ($tasks as $task) <div class="{{ $task->urgent ? 'border-red' : 'border-gray' }}"> <h3>{{ $task->title }}</h3> @if ($task->completed) <p>Status: Done</p> @else <p>Status: In Progress</p> @endif </div> @empty <p>You have no tasks! Enjoy your day.</p> @endforelse
@if or @foreach must be closed with an @endif or @endforeach. If you forget, you'll see a syntax error in your browser.$task outside of your @foreach block.We’ve moved from static HTML to dynamic templates. By mastering these Blade directives:
@if, @elseif, and @else to handle conditional UI states.@foreach to iterate over data collections.@forelse to elegantly handle empty states, ensuring your users never see a blank, confusing page.These tools are the bread and butter of Laravel development. Once these become muscle memory, building complex interfaces becomes significantly faster.
Up next: We will combine everything we've learned to finalize the Task Manager: Building the User Interface.
Learn how to use Blade to render dynamic data in Laravel. Discover how to create .blade.php files, use echo syntax, and implement essential directives.
Read moreLearn to customize Laravel validation error messages to provide clear, helpful feedback to your users. Master field-specific errors and language files.
Mastering Blade Directives for Loops and Conditionals
Protecting Routes with Middleware
Understanding CSRF Protection
Preventing Mass Assignment
Task Manager: Securing the Application
Introduction to Route Model Binding
Updating Existing Records
Deleting Records
Using Named Routes
Task Manager: Completing CRUD Functionality
Introduction to Database Relationships
Querying Related Data
Handling File Uploads
Using Flash Messages for User Feedback
Task Manager: Adding Status and Priorities
Introduction to Artisan Commands
Debugging with Laravel Tinker
Understanding Service Providers
Using View Composers
Task Manager: Refactoring for Clean Code
Introduction to Testing
Testing Forms and Validation
Using Database Transactions
Handling Global Exceptions
Preparing for Production
Environment Security Best Practices
Managing Assets in Production
Task Manager: Deployment Preparation