Laravel 5 Message Bags: Getting an Array of Messages With getMessages

April 21, 2018 —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.

#Signature

The signature of the getMessages method is:

1public function getMessages();

#Example Use

The following examples demonstrate the basic usage of the getMessages method:

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

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.