Laravel ViewErrorBag Public API: hasBag

November 29, 2016 —John Koster

hasBag($key = 'default')

The hasBag method is used to determine if a MessageBag instance exists within the ViewErrorBag instance with the given $key. The $key is set to default unless it is changed. The following example will highlight the usage of the hasBag method.

1<?php
2 
3use Illuminate\Support\ViewErrorBag;
4use Illuminate\Support\MessageBag;
5 
6// Create a new ViewErrorBag instance.
7$viewErrorBag = new ViewErrorBag;
8 
9// Check if the 'default' MessageBag instance
10// exists.
11//
12// false
13$doesExist = $viewErrorBag->hasBag();
14$doesExist = $viewErrorBag->hasBag('default');
15 
16// Check if a 'payments' MessageBag instance
17// exists.
18//
19// false
20$doesExist = $viewErrorBag->hasBag('payments');
21 
22// Add a new 'payments' MessageBag instance.
23$viewErrorBag->put('payments', new MessageBag);
24 
25// Check again if the 'payments' MessageBag instance
26// exists.
27//
28// true
29$doesExist = $viewErrorBag->hasBag('payments');

#Continue Reading

This article is part of a mini-series all about Laravel's View Error Bags. Each of these articles can be found here:

Some absolutely amazing
people

The following amazing people help support this site and my open source projects ♥️
If you're interesting in supporting my work and want to show up on this list, check out my GitHub Sponsors Profile.