Source: barryvanveen.nl

Laravel Blade @each directive

Category: Laravel

The relatively unknown @each function in a very powerful way to loop over data in Laravel Blade templates. It is powerful and much more elegant than @foreach. Suppose you want to loop over all comments on your article.

[Optional] The view partial that will be rendered if the array or collection is empty.

So, rendering a list of articles with their respective comments would look something like this: @each('articles.item', $articles, 'article', 'articles.no-items') {{ $article->title} @each('comments.item', $article->comments, 'comment', 'comments.no-items') {{ comment->author }} wrote: {{ comment->text}}.
Newsletter

Get the latest Laravel/PHP jobs, events and curated articles straight to your inbox, once a week

Fathom Analytics | Fast, simple and privacy-focused website analytics. Fathom Analytics | Fast, simple and privacy-focused website analytics.
Achieve superior email deliverability with ToastMail! Our AI-driven tool warms up inboxes, monitors reputation, and ensures emails reach their intended destination. Sign up today for a spam-free future. Achieve superior email deliverability with ToastMail! Our AI-driven tool warms up inboxes, monitors reputation, and ensures emails reach their intended destination. Sign up today for a spam-free future.
Community Partners