Laravel 5 Message Bags: Retrieving Messages With a Provided Format With all

April 21, 2018 —John Koster

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.

#Signature

The signature of the all method is:

1public function all(
2 $format = null
3);

#Example Use

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

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

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

1The message was: :message
1$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
2 0 => string 'The message was: First Message'
3 1 => string 'The message was: Second Message'
4 2 => string 'The message was: Third Message'
5 3 => string 'The message was: Fourth 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.