By 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 $viewErrorBag
17$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 empty
15 protected 'format' => string ':message' (length=8)
#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:
- An Introduction to Laravel View Error Bags
- Laravel ViewErrorBag Public API: count
- Laravel ViewErrorBag Public API: getBag
- Laravel ViewErrorBag Public API: getBags
- Laravel ViewErrorBag Public API: hasBag
- Laravel ViewErrorBag Public API: put
∎