Search

Laravel MessageBag Public API: all

November 29, 2016 —John Koster

all($format = null)

The all method can be used to retrieve all the messages according to a given $format. The $format is null by default, which will cause the all method to use the format stored within the MessageBag instance (which can be retrieved using the getFormat method). Alternatively, developers can specify their own $format to customize the results each time the all method is executed. The all method will return an array containing all of the formatted messages.

The following code will create a new MessageBag instance with some default messages:

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');

The following code will retrieve all of the messages with the given format:

1The message was: :message
1<?php
2 
3$messages = $messageBag->all('The message was: message');

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

1array (size=4)
2 0 => string 'The message was: First Message' (length=30)
3 1 => string 'The message was: Second Message' (length=31)
4 2 => string 'The message was: Third Message' (length=30)
5 3 => string 'The message was: Fourth Message' (length=31)

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