NewWe open-sourced 50+ Laravel packages
Custom AI apps, agents and automation — Roundly ConsultingRoundly
All packages
Enums for Laravel

Equality checks

Compare a case against one or several others in readable code:

$category = NewsCategory::HotNews;

$category->is(NewsCategory::HotNews);    // true
$category->isNot(NewsCategory::HotNews); // false

$category->isIn([NewsCategory::HotNews, NewsCategory::RegularNews]);        // true
$category->isNotIn([NewsCategory::RegularNews, NewsCategory::PrivateNews]); // true

Comparisons are identity checks (===). is() and isNot() are typed to the enum itself, and isIn() / isNotIn() take a list of cases — an array of raw values never matches. Resolve raw input to a case first with from(), tryFromName() or tryFromLabel():

$category->isIn(['hot-news']);                       // false — raw values never match
$category->isIn([NewsCategory::from('hot-news')]);   // true

In practice

Filter a collection of models by category:

$public = [NewsCategory::HotNews, NewsCategory::RegularNews];

$articles = Article::query()->latest()->get()
    ->filter(fn (Article $article): bool => $article->category->isIn($public));

Or keep a policy readable:

use App\Enums\NewsCategory;
use App\Models\Article;
use App\Models\User;

final class ArticlePolicy
{
    public function view(?User $user, Article $article): bool
    {
        return $user !== null || $article->category->isNot(NewsCategory::PrivateNews);
    }
}

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

By donating, you agree to our donation terms.

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.