Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

enadabuzaid's avatar

Laravel Enum cast: compare enum case vs case->value?

Hello

I’m using Enums with Eloquent casts:

// Model: Booking
protected $casts = [
    'booking_type' => BookingType::class,
    'third_party_status' => ThirdPartyStatus::class,
];
enum BookingType: int {
    case REGULAR = 0;
    case TOURIST = 1;

    public function label(): string {
        return match ($this) {
            self::REGULAR => 'Regular',
            self::TOURIST => 'Tourist',
        };
    }
}
enum ThirdPartyStatus: int {
    case FAILED = 0;
    case CONFIRMED = 1;
    case PENDING = 2;

    public function label(): string {
        return match ($this) {
            self::FAILED => 'Failed',
            self::CONFIRMED => 'Confirmed',
            self::PENDING => 'Pending',
        };
    }
}

Question: When checking a casted enum attribute, which comparison is correct and why?

if ($booking->third_party_status === ThirdPartyStatus::CONFIRMED) {
    // ...
}

// or
if ($booking->third_party_status === ThirdPartyStatus::CONFIRMED->value) {
    // ...
}
  • Does Eloquent always return an enum instance for casted attributes?

  • When should I use ->value?

0 likes
2 replies
LaryAI's avatar
Level 58

Great questions! Let’s clarify how Laravel handles enum casting and how you should compare them.

1. Does Eloquent always return an enum instance for casted attributes?

Yes, if you use native PHP enums in your $casts array (as you did), Eloquent will always return an instance of the enum for that attribute when you access it from your model.

Example:

$booking = Booking::find(1);
var_dump($booking->third_party_status instanceof ThirdPartyStatus); // true

2. Which comparison is correct?

Given your enum cast, you should compare enum instances, not their values:

if ($booking->third_party_status === ThirdPartyStatus::CONFIRMED) {
    // This is correct!
}

This works because both sides are instances of the same enum and the same case.

Do NOT compare the enum instance to its value directly:

if ($booking->third_party_status === ThirdPartyStatus::CONFIRMED->value) {
    // This will always be false!
}

Here, you’re comparing an enum instance to an integer, which will never be equal.

3. When should I use ->value?

Use ->value when you need the raw value (e.g., for database queries, JSON output, or comparisons with non-enum data):

// Example: Querying by value
$confirmedBookings = Booking::where('third_party_status', ThirdPartyStatus::CONFIRMED->value)->get();

Or, if you want to output the value:

echo $booking->third_party_status->value; // outputs 1

Summary Table

Use Case Comparison Syntax
Comparing model attribute to enum case $booking->third_party_status === ThirdPartyStatus::CONFIRMED
Comparing to raw value $booking->third_party_status->value === 1
Querying by value ->where('third_party_status', ThirdPartyStatus::CONFIRMED->value)

In short:

  • Use enum cases for comparisons in PHP (=== with enum instances).
  • Use ->value when you need the underlying value (e.g., for queries or output).

Let me know if you need more examples!

Glukinho's avatar
  1. the correct is: $booking->third_party_status === ThirdPartyStatus::CONFIRMED Because this is what enums are for.

  2. Does Eloquent always return an enum instance for casted attributes? - yes (except null values), otherwise why use the enum cast?

  3. When should I use ->value? - when you need raw value of a field for some reason.

Enums are pretty convenient, the less you use raw values the better.

1 like

Please or to participate in this conversation.