PHP 8.1 Enumerations and Laravel

Home Blogs
PHP 8.1 Enumerations and Laravel

Managing enumerations in databases has always been a daunting task, leading to maintenance issues and lack of developer transparency. However, with the advent of PHP 8.1, there's a new solution that empowers developers to handle enumerations seamlessly on the backend. In this blog post, we'll explore how PHP 8.1 enumerations, combined with the power of Laravel, revolutionise enumeration management and enhance code quality.

An example where it would be a good idea to reach for PHP enums would be Order Statuses. Similar to switch statements, you simply define the cases which are available.

	
	<?php

	enum OrderStatuses 
	{
		case PENDING;
		case PROCESSING;
		case COMPLETE;
	}
	

However, this can be improved further by utilising backed enums, backed enums not only enhance storing and retrieving enum values but also works seamlessly with Laravel's casting functionality. This is done by defining the enum as a string type and assigning corresponding values.

	
	<?php

	enum OrderStatuses: string
	{
		case PENDING = 'pending';
		case PROCESSING = 'processing';
		case COMPLETE = 'complete';
	}
	

Now let's have a look at how we can utilise backed enums with Laravel's casting. Imagine we have an `orders` table with a `status` column, we can provide the $casts array on the Eloquent Model and by telling Laravel to cast the status column to the OrderStatus enum we will be provided with an instance of the OrderStatus enum. This approach ensures consistency and prevents invalid values from being stored, reducing the risk of data integrity issues.

	
	<?php

	class Order extends Model 
	{
		protected $casts = [
			'status' => OrderStatus::class,
		];
	}
	

One of the significant advantages of using enums is their ability to enforce strict type checks. This is incredibly useful when trying to use conditionals.

	
	if ($order->status === OrderStatus::PENDING) {
		echo 'Your order is pending!';
	}
	

Another great thing about enums is that they can have their own method definitions. So if you wanted to show a label for the status with a different colour for each order status, you can do the following

	
	<?php

	enum OrderStatuses: string
	{
		case PENDING = 'pending';
		case PROCESSING = 'processing';
		case COMPLETE = 'complete';

		public function colour(): string
		{
			return match ($this) {
				OrderStatus::PENDING => 'blue',
				OrderStatus::PROCESSING => 'amber',
				OrderStatus::COMPLETE => 'green',
			};
		}
	}
	

Then this can be used in your blade file

	
	<span style="color: {{ $order->status->colour() }};">{{ $order->status->value }}</span>
	

By harnessing the power of PHP enumerations and leveraging Laravel's casting feature, developers can streamline enumeration management. The combination of enum declarations, automatic casting, and type safety enhances code readability and reduces the chances of runtime errors. Embracing these advancements empowers developers to build robust and maintainable applications.