Search

Laravel 5 View Error Bags: Determining if a Message Bag Exists With hasBag

April 21, 2018 —John Koster

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.

#Signature

The signature of the hasBag method is:

1public function hasBag(
2 $key = 'default'
3);

#Example Use

The following example demonstrates the usage of the hasBag method is:

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

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.