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 instance10// exists.11//12// false13$doesExist = $viewErrorBag->hasBag();14$doesExist = $viewErrorBag->hasBag('default');15 16// Check if a 'payments' MessageBag instance17// exists.18//19// false20$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 instance26// exists.27//28// true29$doesExist = $viewErrorBag->hasBag('payments');
This article is part of a mini-series all about Laravel's View Error Bags. Each of these articles can be found here:
∎
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.