By John Koster
The keys
method is used to retrieve all of keys stored inside the MessageBag
instance. The following code example demonstrate the usage of the keys
method:
1<?php
2
3use Illuminate\Support\MessageBag;
4
5// Create a new MessageBag instance.
6$messageBag = new MessageBag;
7
8// Add items to the MessageBag
9$messageBag->add('first', 'First Message');
10$messageBag->add('second', 'Second Message');
11$messageBag->add('third', 'Third Message');
12$messageBag->add('fourth', 'Fourth Message');
13
14// Get the MessageBag keys.
15$keys = $messageBag->keys();
After the above code has executed, the $keys
variable will be an array that contains a value similar to the following output:
1array (size=4)
2 0 => string 'first' (length=5)
3 1 => string 'second' (length=6)
4 2 => string 'third' (length=5)
5 3 => string 'fourth' (length=6)
It can be seen that the results contain only the keys that were added to the MessageBag
instance.
#Continue Reading
This article is the start of a mini-series about Laravel's ErrorMessageBag
component. Click through the rest of the articles to continue reading:
- An Introduction to Laravel Message Bags
- Laravel MessageBag Public API: add
- Laravel MessageBag Public API: all
- Laravel MessageBag Public API: any
- Laravel MessageBag Public API: count
- Laravel MessageBag Public API: first
- Laravel MessageBag Public API: get
- Laravel MessageBag Public API: getFormat
- Laravel MessageBag Public API: getMessageBag
- Laravel MessageBag Public API: getMessages
- Laravel MessageBag Public API: has
- Laravel MessageBag Public API: isEmpty
- Laravel MessageBag Public API: jsonSerialize
- Laravel MessageBag Public API: keys
- Laravel MessageBag Public API: merge
- Laravel MessageBag Public API: setFormat
- Laravel MessageBag Public API: toArray
- Laravel MessageBag Public API: toJson
- Laravel MessageBag Public API: __toString
∎