Laravel 5 Message Bags: Getting all of the Message Keys With keys

April 21, 2018 —John Koster

The keys method is used to retrieve all of keys stored inside the MessageBag instance.

#Signature

The signature of the keys method is:

1public function keys();

#Example Use

The following code example demonstrate the usage of the keys method:

1use Illuminate\Support\MessageBag;
2 
3// Create a new MessageBag instance.
4$messageBag = new MessageBag;
5 
6// Add items to the MessageBag
7$messageBag->add('first', 'First Message');
8$messageBag->add('second', 'Second Message');
9$messageBag->add('third', 'Third Message');
10$messageBag->add('fourth', 'Fourth Message');
11 
12// Get the MessageBag keys.
13$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
2 0 => string 'first'
3 1 => string 'second'
4 2 => string 'third'
5 3 => string 'fourth'

It can be seen that the results contain only the keys that were added to the MessageBag instance.

Some absolutely amazing
people

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.