The Blog

Laravel 5

Laravel 5

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

John Koster
John Koster
April 21, 2018

The count method in Laravel's MessageBag class returns the total number of messages stored within the instance. It is implemented to satisfy PHP's Countable interface. Use this method to get the count of messages in a MessageBag instance.

Laravel 5

Laravel 5 Message Bags: Returning the Message Bag Instance With getMessageBag

John Koster
John Koster
April 21, 2018

The getMessageBag method is used to get a reference to the MessageBag instance. It is used to satisfy the requirements of the "Illuminate\Contracts\Support\MessageProvider" interface. Here is an example of how it can be used.

Laravel 5

Laravel 5 Message Bags: Setting the Message Format With setFormat

John Koster
John Koster
April 21, 2018

The setFormat method in Laravel's MessageBag class allows developers to customize the format used by all methods of the instance. By providing a value for the $format parameter, developers can define the new format. Calling setFormat without a value will reset the format to the default value. Learn how to use this method with examples in the provided documentation.

Laravel 5

Laravel 5: Message Bags

John Koster
John Koster
April 21, 2018

The "Illuminate\Support\MessageBag" class is a key/value storage system for messages. It can be used to format messages for HTML forms, emails, etc. The class implements the "Illuminate\Contracts\Support\MessageProvider" interface and is extended or implemented by various other classes, such as the "Illuminate\Contracts\Validation\ValidationException" and the "Illuminate\Http\RedirectResponse".

Laravel 5

Laravel 5 View Error Bags: Add a New Message Bag Instance to the View Error Bag With put

John Koster
John Koster
April 21, 2018

The put method is used to add a new MessageBag instance to a ViewErrorBag instance, with a specified key. It can also be used to remove all messages from a MessageBag instance and replace them with new ones. Messages can be set dynamically by assigning a new MessageBag instance to a property of the ViewErrorBag instance.

Laravel 5

Laravel 5 View Error Bags: Determining if a Message Bag Exists With hasBag

John Koster
John Koster
April 21, 2018

Learn how to use the hasBag method in Laravel to determine if a MessageBag instance exists within a ViewErrorBag instance. The method can be called with a key parameter, which is set to default by default. Check out the code example in the article to see how it works.

Laravel 5

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

John Koster
John Koster
April 21, 2018

The any method is a convenience shortcut to check if the error message bag contains any messages. It returns a boolean value indicating whether there are messages present or not. Instead of using count() > 0, you can simply use any() to perform certain actions when there are error messages.

Laravel 5

Laravel 5 View Error Bags: Getting all Message Bag Instances With getBags

John Koster
John Koster
April 21, 2018

The getBags method retrieves all the MessageBag instances within the ViewErrorBag instance and returns an associative array. This allows you to retrieve and manipulate error messages in a convenient way. To use it, create a new ViewErrorBag instance, populate it with MessageBag instances, and then call the getBags method to retrieve the array of message bags.

Laravel 5

Laravel 5 View Error Bags: Getting the Message Bag Instance With getBag

John Koster
John Koster
April 21, 2018

Learn how to use the getBag method to retrieve a MessageBag instance based on a provided key. If a MessageBag instance does not exist, a new instance will be returned instead. See code examples that demonstrate the usage and behavior of the getBag method, including accessing a MessageBag instance dynamically. Be aware of the behavior where the getBag method does not set the returned MessageBag instance.

Laravel 5

Laravel 5 View Error Bags: Getting the Number of Error Messages With count

John Koster
John Koster
April 21, 2018

The count method in Laravel's MessageBag class returns the number of messages stored within the default instance of MessageBag. It is specifically handled by the ViewErrorBag instance, and needs to be declared within the ViewErrorBag class itself to satisfy PHP's Countable interface requirements. The example usage shows how the count method operates on the default MessageBag instance and how it can be affected by adding or changing the MessageBag instance.

Latest posts

That Escalated Quickly: All the New Things

The past six months or so have been incredibly busy. What started as a new article series about cust...

Read more
Troubleshooting a Statamic Illegal Offset Type Error when Viewing Collection Entries in the Control Panel

In this post I talk about how I resolved a mysterious illegal offset type error when viewing collect...

Read more
Creating Simple HTTP Redirect Routes from a Statamic Site

Generating a custom Laravel routes file from a Statamic website to redirect to a new domain.

Read more
Disabling Vite File Hashes

Disabling file hashes in Vite output can be accomplished by modifying your project's vite.config.js

Read more
Implementing a Custom Laravel Blade Precompiler for Volt and Livewire

Learn how to implement a custom component compiler for Laravel's Blade templating language in this p...

Read more
Creating a Hybrid Cache System for Statamic: Part Five

Part 5 of 6 covers implementing a cache namespace and labeling system, which we can use to target mu...

Read more