Published in Laravel 5

Laravel 5 View Error Bags: Determining if There Are Any Error Messages With any

By John Koster

The any method can be used to determine if the error message bag contains any messages. It is a convenience method and acts a shortcut to calling the count method within a conditional statement. The any method returns a boolean value.

#Signature

The signature of the any method is:

1public function any();

#Example Use

The following examples are equivalent:

1use Illuminate\Support\ViewErrorBag;
2use Illuminate\Support\MessageBag;
3
4// Create a new ViewErrorBag instance.
5$viewErrorBag = new ViewErrorBag;
6
7// Create a new MessageBag instance.
8$messageBag = new MessageBag([
9 'username' => [
10 'The username is required.'
11 ]
12]);
13
14// Add the new MessageBag instance to
15// the ViewErrorBag
16$viewErrorBag->put('formErrors', $messageBag);
17
18if ($viewErrorBag->count() > 0) {
19 // Perform some action when there are messages.
20}
21
22if ($viewErrorBag->any()) {
23 // Perform some action when there are messages.
24}