Published in Laravel Training Resources

Exploring Laravel Custom Casts with PHP Enums

Cultivate Clarity and Precision with Laravel Custom Casts and PHP Enums. Dive into the world of Laravel Custom Casts and harness the expressiveness of PHP Enums.

By Shane Rosenthal

Laravel, a popular PHP framework, offers a plethora of features and tools to streamline web application development. One of the lesser-known gems within Laravel is the Custom Casts feature. Custom Casts allow you to convert attributes of your Eloquent models to and from various formats. In this article, we'll dive into Custom Casts and explore how to use PHP Enums inside a Custom Cast.

Understanding Custom Casts

Custom Casts are a powerful feature in Laravel that allow you to manipulate the data stored in your database by defining custom attribute casting logic. By default, Laravel supports common data types such as dates, JSON, and arrays. However, there are cases where you may want to cast data into a custom format that isn't supported out of the box, which is where Custom Casts come into play.

To create a Custom Cast, you'll need to define a class that implements the Cast contract, which includes methods for casting attributes to and from the desired format.

Introducing PHP Enums

Before delving into using PHP Enums in Laravel Custom Casts, let's briefly understand what PHP Enums are. Enums are a way to define a fixed set of constants that represent possible values for a variable. They provide a type-safe and expressive way to work with a predefined list of options.

PHP introduced support for Enums in version 8.1, and they have quickly become a valuable tool for developers to ensure code correctness and improve code readability.

Using PHP Enums in Custom Casts

Now that we have a basic understanding of both Laravel Custom Casts and PHP Enums, let's explore how we can use Enums within a Custom Cast.

Assume you have a Laravel Eloquent model representing a Product, and one of its attributes is the product's status, which can be one of three values: AVAILABLE, SOLD_OUT, or DISCONTINUED. You can define a PHP Enum to represent these statuses:

1enum ProductStatus {
2 case AVAILABLE;
3 case SOLD_OUT;
4 case DISCONTINUED;
5}

Next, let's create a Custom Cast that will handle the casting of the Product's status attribute:

1use Illuminate\Contracts\Database\Eloquent\CastsAttributes;
2 
3class ProductStatusCast implements CastsAttributes {
4 public function get($model, $key, $value, $attributes) {
5 return new ProductStatus($value);
6 }
7 
8 public function set($model, $key, $value, $attributes) {
9 return $value->value;
10 }
11}

In the code above, we define a Custom Cast named ProductStatusCast that converts the attribute from the database value to a ProductStatus Enum when retrieving it, and vice versa when setting it.

Using Enums in HTML Templates

Now that we have our Custom Cast in place, we can easily use the Product's status attribute in our HTML templates. Let's assume you want to display the product's status in a view:

1<div class="product-status">
2 <p>Product Status: {{ $product->status }}</p>
3</div>

Thanks to our Custom Cast, the $product->status attribute will automatically be cast to its corresponding ProductStatus Enum, making it easy to display in your HTML template.

Conclusion

Laravel Custom Casts are a versatile tool for handling attribute casting in your Eloquent models. When combined with PHP Enums, you can create robust and type-safe solutions for managing data. This not only improves code readability but also reduces the chances of runtime errors. As you explore Laravel's features, don't overlook the power of Custom Casts and Enums for enhancing your web application development.