---
title: "Case lookups — Enums for Laravel | Roundly"
description: "Resolve a case by name or readable label with fromName(), fromLabel() and their tryFrom* variants; check existence with hasName() and hasValue()."
url: https://roundly-consulting.com/open-source/docs/enums-for-laravel/lookups
language: en
---

[All packages](https://roundly-consulting.com/open-source.md)

[Enums for Laravel](https://roundly-consulting.com/open-source/docs/enums-for-laravel.md)

# Case lookups

Resolve a case from its name or its readable label, and check whether one exists. The from\* variants throw RoundlyConsulting\\Enums\\Exceptions\\EnumException when nothing matches; the tryFrom\* variants return null — and short-circuit on a null argument:

```php
NewsCategory::fromName('HotNews');       // NewsCategory::HotNews
NewsCategory::tryFromName('Missing');    // null
NewsCategory::tryFromName(null);         // null

NewsCategory::fromLabel('Hot News');     // NewsCategory::HotNews
NewsCategory::tryFromLabel('Nope');      // null

NewsCategory::hasName('HotNews');        // true
NewsCategory::hasValue('hot-news');      // true
```

| Method | Returns | When nothing matches |
| --- | --- | --- |
| `fromName(string $name)` | `static` | Throws EnumException. |
| `tryFromName(?string $name)` | `?static` | null (also for a null argument). |
| `fromLabel(string $label)` | `static` | Throws EnumException. |
| `tryFromLabel(?string $label)` | `?static` | null (also for a null argument). |
| `hasName(string $name)` | `bool` | false |
| `hasValue(string\|int $value)` | `bool` | false — strict comparison against values() (case names on a pure enum). |

Name lookups are exact and case-sensitive (hotnews doesn’t match HotNews). Label lookups compare against readable(), so they match the label in the active locale. hasValue() compares strictly against values() — '5' never matches an int-backed 5 — and on a pure enum it matches the case name.

When several cases share a label — 'in-progress' and 'in\_progress' both read “In Progress”, or two values translate to the same text — fromLabel() and tryFromLabel() return the first declared case. The match is deterministic, but the later twins can’t be reached by label, so give cases that must round-trip through labels distinct ones.

## Looking up by value

Value lookups stay native — PHP’s own from() and tryFrom() on backed enums:

```php
NewsCategory::from('hot-news');     // NewsCategory::HotNews — native PHP
NewsCategory::tryFrom('breaking');  // null — native PHP
```

## Importing data

tryFromLabel() is made for matching human-typed text — a spreadsheet column, a CSV export, an API field — back to a case without try/catch:

```php
use App\Enums\NewsCategory;
use App\Models\Article;

foreach ($rows as $row) {
    $category = NewsCategory::tryFromLabel($row['category'] ?? null);

    if ($category === null) {
        continue; // unknown label: skip or report the row
    }

    Article::create([
        'title' => $row['title'],
        'category' => $category,
    ]);
}
```

## Show your open-source love

This package is free and MIT-licensed. If it saves you time, a one-off donation or a Patreon membership keeps it maintained, tested and documented.

[More ways to support, including crypto](https://roundly-consulting.com/support-us.md)

By donating, you agree to our [donation terms](https://roundly-consulting.com/donation-terms.md).

[Support our open source work (opens in a new tab)](https://donate.stripe.com/dRmeVe8FX5PF1Qd9pXcEw00) [Join us on Patreon (opens in a new tab)](https://www.patreon.com/cw/roundly)

## Want this built into your product?

We integrate our packages into custom Laravel and AI builds. Tell us what you're working on and we'll reply within 48 hours.

[Get a quote in 48 hours](https://roundly-consulting.com/contact.md) [Browse all packages](https://roundly-consulting.com/open-source/docs/enums-for-laravel.md)
