@each

You can probably imagine some circumstances in which you’d need to loop over an array or collection and @include a partial for each item. There’s a directive for that.

Let’s say we have a sidebar composed of modules, and we want to incude multiple modules, each with a different title. Take a look at code below

Using view partials in a loop with @each
<!-- resources/views/sidebar.blade.php --> <div class="sidebar"> @each('partials.module', $modules, 'module', 'partials.empty-module') </div> <!-- resources/views/partials/module.blade.php --> <div class="sidebar-module"> <h1>{{ $module->title }}</h1> </div> <!-- resources/views/partials/module.blade.php --> <div class="sidebar-module"> No modules :( </div>

Take a look at that @each syntax. The first parameter is the name of the view partial. The second is the array or collection to iterate over. The third is the variable name that each item will be passed to the view as. And the optional fourth parameter is the view to show if the array or collection is empty.