Laravel MessageBag Public API: getMessages

November 29, 2016 —John Koster

The getMessages method is used to return all of the messages stored within the MessageBag instance. The getMessages method will always return an array. If there are no messages in the message bag, an empty array will be returned.

1<?php
2 
3use Illuminate\Support\MessageBag;
4 
5// Create a new MessageBag instance.
6$messageBag = new MessageBag;
7 
8// Add new messages to the message bag.
9$messageBag->add('hello', 'The first message bag message');
10$messageBag->add('world', 'The second message');
11 
12// Get an array of all the messages.
13$messages = $messageBag->getMessages();

After the above code has executed, the $messages variable will be an array and contain a value similar to the following output:

1array (size=2)
2 'hello' =>
3 array (size=1)
4 0 => string 'The first message bag message' (length=29)
5 'world' =>
6 array (size=1)
7 0 => string 'The second message' (length=18)

#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:

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.