The Blog

Laravel

Laravel MessageBag Public API: has

John Koster
John Koster
November 29, 2016

The has method in Laravel's MessageBag class checks if messages exist for a given key. It returns true if messages exist, and false if not. You can use this method to determine if messages are present before performing any further actions. Check out the rest of the articles in this series to learn more about Laravel's ErrorMessageBag component.

Laravel

Laravel MessageBag Public API: isEmpty

John Koster
John Koster
November 29, 2016

The isEmpty method in Laravel's Collection class can be used to check if the collection contains any items. It returns true if the collection is empty and false otherwise. This can be useful for conditionally executing some code based on whether the collection has items or not. You can learn more about Laravel's ErrorMessageBag component by clicking through the rest of the articles listed in the "Continue Reading" section.

Laravel

Laravel MessageBag Public API: jsonSerialize

John Koster
John Koster
November 29, 2016

The jsonSerialize method of Laravel's ErrorMessageBag allows developers to customize how a class is represented when using the json_encode function. This method internally returns the value of the toArray method, which converts the error messages into an associative array. This article provides a mini-series that explores various public methods of the ErrorMessageBag component in Laravel, such as add, all, any, and more.

Laravel

Laravel MessageBag Public API: keys

John Koster
John Koster
November 29, 2016

The keys method in Laravel allows you to retrieve all the keys stored inside a MessageBag instance. In the code example provided, a MessageBag instance is created and items are added to it. Then, the keys method is called to retrieve an array of keys. The resulting array will contain only the keys that were added. If you want to learn more about Laravel's ErrorMessageBag component, you can check out the mini-series of articles provided in the "Continue Reading" section. These articles cover various aspects of the MessageBag public API.

Laravel

Laravel MessageBag Public API: merge

John Koster
John Koster
November 29, 2016

The merge method in Laravel is used to combine the contents of a MessageBag instance with another array or object that implements the MessageProvider interface. This method modifies the original MessageBag instance and returns a reference to it. In the provided examples, the merge method is used to merge an array with a MessageBag instance and merge two MessageBag instances together. The resulting merged messages can be accessed as an array. This article is part of a series on Laravel's ErrorMessageBag component, exploring various methods in the public API.

Laravel

Laravel MessageBag Public API: setFormat

John Koster
John Koster
November 29, 2016

The setFormat method is used to customize the format used by all methods of the MessageBag instance in Laravel. By passing a $format argument, developers can define the new format. If called without a value, it will reset the format to the default value. This method is part of the Laravel ErrorMessageBag component. Continue reading to learn more about Laravel's MessageBag and its public API methods. The articles cover various methods like add, all, any, count, first, get, getFormat, getMessageBag, getMessages, has, isEmpty, jsonSerialize, keys, merge, toArray, toJson, and __toString.

Laravel

Laravel MessageBag Public API: toArray

John Koster
John Koster
November 29, 2016

The toArray method in Laravel's Collection class converts the collection instance into an array. It not only returns the underlying array but also converts any object instances into arrays. In the provided code example, the $returnedItems variable would contain an array with three elements, including a nested array. If you want to learn more about Laravel's ErrorMessageBag component, check out the rest of the articles in the series. They cover various public API methods, including add, all, any, count, first, get, getFormat, getMessageBag, getMessages, has, isEmpty, jsonSerialize, keys, merge, setFormat, toArray, toJson, and __toString.

Laravel

Laravel MessageBag Public API: toJson

John Koster
John Koster
November 29, 2016

The toJson method in Laravel's ErrorMessageBag class converts the message bag instance into a JSON-encoded string. It internally uses PHP's json_encode function to perform this conversion. You can pass additional options to customize the encoding process by using the $options parameter. By default, the method returns a string without any formatting. However, you can pass the JSON_PRETTY_PRINT constant to get a more human-readable output. If your data structure is deeply nested and exceeds the default depth limit of 512 in json_encode, you can manually serialize the message bag and use json_encode with a custom depth value. This article is part of a mini-series that explores various methods in Laravel's ErrorMessageBag component.

Laravel

Laravel ViewErrorBag Public API: count

John Koster
John Koster
November 29, 2016

Learn how to use the count method in Laravel's View Error Bags to determine the number of messages stored within the default MessageBag instance. This method is specifically handled by the ViewErrorBag instance and can be used to satisfy the requirements of PHP's Countable interface. Understand how adding or changing MessageBag instances affect the count results. Check out the complete mini-series about Laravel's View Error Bags for more information and other APIs you can use.

Laravel

Laravel ViewErrorBag Public API: getBag

John Koster
John Koster
November 29, 2016

The getBag method retrieves a MessageBag instance associated with a provided key. If no instance exists with the key, a new Illuminate\Support\MessageBag instance will be returned. You can also request a MessageBag instance with any key, even if it hasn't been set. However, note that getBag does not set the returned MessageBag instance. You can dynamically access a MessageBag instance from ViewErrorBag by using a property with the intended key. For further reading about Laravel's View Error Bags, check out the mini-series of articles linked in the post.

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