Laravel's new @forelse construct
This blog post was originally published a little while ago. Please consider that it may no longer be relevant or even accurate.
Fun to note that there is a new @forelse construct in Laravel 4.2, something that has apparently just made it's way back into the framework after last making its appearance in Laravel 3. You can have a look at the finer implementation details in the pull request on GitHub but I thought I would go over the feature here, as it's something that I might start using in the right circumstances.
This simple syntax allows you to provide alternate content if you don't have any results for the page to display. It's really nice and simple, but at the same time there are some very good reasons not to use it (in my opinion). For example, take a look at what I would traditionally do in my applications.
What are the benefits to this? As far as I see it, there are two:
-
What you would display when you have no records to display is usually a lot shorter than whatever you would display if there is content. Because of this I feel it is preferable to keep it as your first clause so you know where it is when you need to find it. If it was placed at the bottom you would have to scroll through the actual content which can vary in length.
-
It allows you to provide content around your records if do have any. If I wanted to use a table in the top example I would have to place it around the whole
@forelse
construct and have it display whether or not I have records. The bottom solution allows me to only display a table if I have records and then show something completely difference if I don't.
Of course there are use cases for both and it really depends on what you want to achieve, but it is definitely worth knowing the tools that Blade makes available to you.