November 29, 2016 —John Koster
The getBags
method is used to return an associative array containing all the MessageBag
instances that are currently stored within the ViewErrorBag
instance.
1<?php 2 3use Illuminate\Support\ViewErrorBag; 4use Illuminate\Support\MessageBag; 5 6// Create a new ViewErrorBag instance. 7$viewErrorBag = new ViewErrorBag; 8 9// Create a new MessageBag instance.10$messageBag = new MessageBag([11 'username' => [12 'The username is required.'13 ]14]);15 16// Add some message bags to $viewErrorBag17$viewErrorBag->put('formErrors', $messageBag);18$viewErrorBag->put('paymentErrors', new MessageBag);19 20// Get the message bags as an array.21$messageBags = $viewErrorBag->getBags();
After the above code has executed, the $messageBags
variable would be an array and contain a value similar to the following output:
1array (size=2) 2 'formErrors' => 3 object(Illuminate\Support\MessageBag)[142] 4 protected 'messages' => 5 array (size=1) 6 'username' => 7 array (size=1) 8 ... 9 protected 'format' => string ':message' (length=8)10 'paymentErrors' =>11 object(Illuminate\Support\MessageBag)[143]12 protected 'messages' =>13 array (size=0)14 empty15 protected 'format' => string ':message' (length=8)
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.