Search

Laravel 5 Message Bags: Retrieving the Number of Messages With count

April 21, 2018 —John Koster

The count method returns the total number of messages stored within the MessageBag instance. The count method exists to satisfy the requirements of PHP's Countable interface.

#Signature

The signature of the count method is:

1public function count();

#Example Use

The following example shows the usage of the count method:

1use Illuminate\Support\MessageBag;
2 
3// Create a new MessageBag instance.
4$messageBag = new MessageBag;
5 
6// Add a few messages to the MessageBag instance.
7$messageBag->add('first', 'This is a message');
8$messageBag->add('first', 'I am also a message -.- ');
9$messageBag->add('second', 'Can I join, too?');
10 
11// 3
12$count = $messageBag->count();

It can be seen in the above example that the count message counts the number of messages (which is 3), not necessarily the number of keys (which is 2).