Laravel 5 Message Bags: Checking if Messages Exist for a Specific Input Key With has

April 21, 2018 —John Koster

The has method can be used to determine if messages exist within the MessageBag instance for a given $key. The has method returns true if messages exist for the given $key, and returns false if no messages exist.


The signature of the has method is:

1public function has(
2 $key

The following examples demonstrate the usage of the has method:

1use Illuminate\Support\MessageBag;
3// Create a new MessageBag instance.
4$messageBag = new MessageBag;
6// false
7$hasMessage = $messageBag->has('test');
9// Add the message to the message bag.
10$messageBag->add('test', 'Example message');
12// true
13$hasMessage = $messageBag->has('test');

Some absolutely amazing

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.