Laravel 5 Message Bags: Setting the Message Format With setFormat

April 21, 2018 —John Koster

The setFormat is the logical opposite of the getFormat method. It allows developers to customize the format used by all methods of the MessageBag instance. It defines a $format parameter, whose argument will be used as the value of the new format. If the setFormat method is called without supplying a value for $format, it will reset the format to the default value (because of the parameter's default value).

#Signature

The signature of the setFormat method is:

1public function setFormat(
2 $format = ':message'
3);

#Example Use

The following example demonstrates the usage of the setFormat method:

1use Illuminate\Support\MessageBag;
2 
3// Create a new MessageBag instance.
4$messageBag = new MessageBag;
5 
6// :message
7$format = $messageBag->getFormat();
8 
9// Change the format.
10$messageBag->setFormat('<li>:message</li>');
11 
12// <li>:message</li>
13$format = $messageBag->getFormat();
14 
15// Reset the format.
16$messageBag->setFormat();
17 
18// :message
19$format = $messageBag->getFormat();

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.