Published in Laravel

Laravel MessageBag Public API: keys

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: