PHPackages                             matthewpageuk/laravel-bitty-enums - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. matthewpageuk/laravel-bitty-enums

ActiveLibrary

matthewpageuk/laravel-bitty-enums
=================================

This package helps you use bitwise enums in PHP and Laravel.

v1.0.1(2y ago)912.1k↓50%MITPHPPHP ^8.1

Since Feb 9Pushed 2y ago1 watchersCompare

[ Source](https://github.com/MatthewPageUK/laravel-bitty-enums)[ Packagist](https://packagist.org/packages/matthewpageuk/laravel-bitty-enums)[ Docs](https://github.com/matthewpageuk/laravel-bitty-enums)[ GitHub Sponsors]()[ RSS](/packages/matthewpageuk-laravel-bitty-enums/feed)WikiDiscussions main Synced 1mo ago

READMEChangelog (3)Dependencies (7)Versions (4)Used By (0)

Laravel Bitwise Enums
=====================

[](#laravel-bitwise-enums)

[![Latest Version on Packagist](https://camo.githubusercontent.com/d82293afe4c2ee0c19fc416f83971cdb52c3c789cbf737a6d086940db79ca062/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f6d61747468657770616765756b2f6c61726176656c2d62697474792d656e756d732e7376673f7374796c653d666f722d7468652d6261646765)](https://packagist.org/packages/matthewpageuk/laravel-bitty-enums)[![GitHub Tests Action Status](https://camo.githubusercontent.com/fcbd4d85ddf948673778b2ecfb27f34bc93dcb5618c211dd4a2e46bbea145f33/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f616374696f6e732f776f726b666c6f772f7374617475732f6d61747468657770616765756b2f6c61726176656c2d62697474792d656e756d732f72756e2d74657374732e796d6c3f6272616e63683d6d61696e266c6162656c3d7465737473267374796c653d666f722d7468652d6261646765)](https://github.com/matthewpageuk/laravel-bitty-enums/actions?query=workflow%3Arun-tests+branch%3Amain)[![GitHub Code Style Action Status](https://camo.githubusercontent.com/eb32d910cde817837d206790cb2f18183075a1f5a0d59c01d0d18ee9c8a07102/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f616374696f6e732f776f726b666c6f772f7374617475732f6d61747468657770616765756b2f6c61726176656c2d62697474792d656e756d732f6669782d7068702d636f64652d7374796c652d6973737565732e796d6c3f6272616e63683d6d61696e266c6162656c3d636f64652532307374796c65267374796c653d666f722d7468652d6261646765)](https://github.com/matthewpageuk/laravel-bitty-enums/actions?query=workflow%3A%22Fix+PHP+code+style+issues%22+branch%3Amain)[![Total Downloads](https://camo.githubusercontent.com/f9f09fba4aabf710dc1f81a0f7758d35457d6ade7ca485e5e150fd2850eaa852/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f6d61747468657770616765756b2f6c61726176656c2d62697474792d656e756d732e7376673f7374796c653d666f722d7468652d6261646765)](https://packagist.org/packages/matthewpageuk/laravel-bitty-enums)[![GitHub Issues](https://camo.githubusercontent.com/30f67dac4e514196d2f54e805242d22039821806d54edf36584429e0a541b826/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f6973737565732f6d61747468657770616765756b2f6c61726176656c2d62697474792d656e756d732e7376673f7374796c653d666f722d7468652d6261646765)](https://github.com/matthewpageuk/laravel-bitty-enums/issues)

This package helps you use bitwise enums in your Laravel application if you choose to, think before you do. It provides an Enum Container, a trait for your model query scopes and a model attribute cast.

You can think of this as a hasMany relationship but using a single integer column to store the data.

[![bitty1](https://private-user-images.githubusercontent.com/46349796/303944571-faed58e5-8f08-456c-a24e-9a7f17870f15.png?jwt=eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJnaXRodWIuY29tIiwiYXVkIjoicmF3LmdpdGh1YnVzZXJjb250ZW50LmNvbSIsImtleSI6ImtleTUiLCJleHAiOjE3NzUzNjM3NTksIm5iZiI6MTc3NTM2MzQ1OSwicGF0aCI6Ii80NjM0OTc5Ni8zMDM5NDQ1NzEtZmFlZDU4ZTUtOGYwOC00NTZjLWEyNGUtOWE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BmMDNlYTUmWC1BbXotU2lnbmVkSGVhZGVycz1ob3N0In0.V6I0Hyndv3LlZNLUg3M4LhXtFHdJnOgJP9CLVGxzAVo)

Installation
------------

[](#installation)

You can install the package into your Laravel project via composer:

```
composer require matthewpageuk/laravel-bitty-enums
```

Usage
-----

[](#usage)

- [Create an Enum](#create-an-enum)
- [Using the Bitty Enum Container](#using-the-bitty-enum-container)
- [Model Attribute Cast](#model-attribute-cast)
- [Scoped Queries](#scoped-queries)
- [Config settings](#config-settings)

Create an Enum
--------------

[](#create-an-enum)

You can create a new `enum` using the `bitty-enum:make` Artisan command. This command will create a new enum class in the `app/Enums` directory with the cases you supply. It will ensure the values and names are suitable for use with the package.

```
php artisan bitty-enum:make Colours
```

To use your own `enums` with this package they must :

- Implement the `MatthewPageUK\BittyEnums\Contracts\BittyEnum` interface.
- Return type `int`
- Values be a power of 2 starting from 1 in order

Invalid enums will throw an `BittyEnumException` when used with the container.

***There is a current limit of 16 cases (bits) per enum. This can be overiding in your config files.***

### Example of a Colour enum:

[](#example-of-a-colour-enum)

```
use MatthewPageUK\BittyEnums\Contracts\BittyEnum;

enum Colour: int implements BittyEnum
{
    case Red = 1;
    case Green = 2;
    case Blue = 4;
    case White = 8;
    case Black = 16;
    case Pink = 32;
}
```

Using the Bitty Enum Container
------------------------------

[](#using-the-bitty-enum-container)

The container is used to store the selected enum values. It is a wrapper around the integer value and provides methods to manage and check the values. It also performs validation on the values you set to prevent accidental misuse.

### Creating a container

[](#creating-a-container)

You can create a new container using the Contract binding in the Laravel app.

```
use App\Enums\Colour;
use MatthewPageUK\BittyEnums\Contracts\BittyContainer;

$container = app()->make(BittyContainer::class)->setClass(Colour::class);
```

This will create a container suitable for the `Colour` enum.

You can also use the `MatthewPageUK\BittyEnums\Support\Container` directly.

```
use MatthewPageUK\BittyEnums\Support\Container as BittyContainer;

$favouriteColours = (new BittyContainer(Colour::class))
    ->set(Colour::Red)
    ->set(Colour::Green)
    ->set(Colour::Blue);
```

### Example usage

[](#example-usage)

```
// Set values
$favouriteColours = app()->make(BittyContainer::class)
    ->setClass(Colour::class)
    ->set(Colour::Red)
    ->set(Colour::Green)
    ->set(Colour::Blue);

// Passing an array of values
$favouriteColours = app()->make(BittyContainer::class)
    ->setClass(Colour::class)
    ->set([Colour::Red, Colour::Green, Colour::Blue]);

// Unset a value
$favouriteColours->unset(Colour::Red);

// Check if the container has a value
if ($favouriteColours->has(Colour::Red)) {
    echo 'Red is one of your favourite colours';
}

// Check if the container has any of the values
if ($favouriteColours->hasAny([Colour::Red, Colour::Green])) {
    echo 'You like red or green';
}

// Check if the container has all of the values
if ($favouriteColours->hasAll([Colour::Red, Colour::Green])) {
    echo 'You like red and green';
}

// Pass another container to check if any of the values exist
if ($product->colours->hasAny($favouriteColours)) {
    echo 'This product is available in one of your favourite colours';
}
```

### Container public methods

[](#container-public-methods)

```
public function __construct(string $class, int $selected = 0);

public function clear(): BittyContainer;

public function getChoices(): array;

public function getValue(): int;

public function has(BittyEnum $choice): bool;

public function hasAll(array|BittyContainer $choices): bool;

public function hasAny(array|BittyContainer $choices): bool;

public function set(array|BittyContainer|BittyEnum $choice): BittyContainer;

public function setAll(): BittyContainer;

public function unset(array|BittyContainer|BittyEnum $choice): BittyContainer;
```

### Validation

[](#validation)

The container also perfoms validation on the values you set, throwing a `BittyEnumException` if you try to set an invalid value or have a malformed enum.

Model Attribute Cast
--------------------

[](#model-attribute-cast)

You can cast the `integer` column on your models to a `BittyEnumContainer` using the `MatthewPageUK\BittyEnums\Casts\BittyEnumCast` cast.

You must pass the enum class you intend to use as the second parameter.

Your database column should be a `BIGINT`.

@todo see also bit limit on enum, how many can we have?

### Example Laravel Model

[](#example-laravel-model)

```
Schema::create('products', function (Blueprint $table) {
    $table->id();
    $table->string('name');
    $table->unsignedBigInteger('colours');
    $table->unsignedInteger('price');
    $table->timestamps();
});

use App\Enums\Colour;
use MatthewPageUK\BittyEnums\Casts\BittyEnumCast;

class Product extends Model
{
    protected $casts = [
        'colours' => BittyEnumCast::class . ':' . Colour:class,
    ];
}
```

You can now use the container methods to update and retrieve the enum values direct from your model attribute.

### Example Usage

[](#example-usage-1)

```
$product = Product::find(1);
$product->colours->set(Colour::Blue)->unset(Colours::Red);
$product->save();
```

```
// Check if value exists
$product = Product::find(1);
if ($product->colours->has(Colour::Blue)) {
    echo 'This product is available in blue';
}
```

```
// Check if any of the values exist
$customerPreferences = app()->make(BittyContainer::class)
    ->setClass(Colour::class)
    ->set(Colour::Blue)
    ->set(Colour::Red)
    ->set(Colour::Green);

$product = Product::find(1);
if ($product->colours->hasAny($customerPreferences)) {
    echo 'This product is available in one of the customers preferred colours';
}
```

Scoped Queries
--------------

[](#scoped-queries)

To access the scoped queries in your model you need to use the `MatthewPageUK\BittyEnums\Traits\WithBittyEnumQueryScope` trait.

You should also ensure your model has the [BittyEnumCast](#model-attribute-cast) set on the column you want to query.

### Example Model

[](#example-model)

```
use App\Enums\Colours;
use MatthewPageUK\BittyEnums\Traits\WithBittyEnumQueryScope;

class Product extends Model
{
    use WithBittyEnumQueryScope;

    ...
}
```

### Example Queries

[](#example-queries)

```
// Products with the colour blue
Product::whereBittyEnumHas('colours', Colour::Blue)->get();

// Products with the colour blue or red
Product::whereBittyEnumHasAny('colours', [Colour::Blue, Colour::Red])->get();

// Products with the colour blue and red
Product::whereBittyEnumHasAll('colours', [Colour::Blue, Colour::Red])->get();

// Products without the colour blue
Product::whereBittyEnumDoesntHave('colours', Colour::Blue)->get();

// Products without the colour blue or red
$customerPreferences = new BittyEnumContainer(Colour::class)
    ->set(Colour::Blue)
    ->set(Colour::Red);

Product::whereBittyEnumDoesntHaveAny('colours', $customerPreferences)->get();
```

Methods accepting multiple choices can be an array of `BittyEnum` or a `BittyEnumContainer`.

A `BittyEnumException` will be thrown if you pass the incorrect type or invalid enum to the query scope.

Config settings
---------------

[](#config-settings)

You can set the maximum number of bits for the container in the config file.

```
return [
    'max_bits' => 16,
];
```

Package Testing
---------------

[](#package-testing)

```
composer test
```

Changelog
---------

[](#changelog)

Please see [CHANGELOG](CHANGELOG.md) for more information on what has changed recently.

Contributing
------------

[](#contributing)

Please see [CONTRIBUTING](CONTRIBUTING.md) for details.

Credits
-------

[](#credits)

- [Matthew Page](https://github.com/MatthewPageUK)

License
-------

[](#license)

The MIT License (MIT). Please see [License File](LICENSE.md) for more information.

###  Health Score

31

—

LowBetter than 68% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity31

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity52

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~1 days

Total

3

Last Release

827d ago

Major Versions

v0.0.1-alpha → v1.0.02024-02-11

### Community

Maintainers

![](https://www.gravatar.com/avatar/469f33e92446038e57a58323fe1259aa783be2946a126ad34f8abf9d9f268b17?d=identicon)[MatthewPageUK](/maintainers/MatthewPageUK)

---

Top Contributors

[![MatthewPageUK](https://avatars.githubusercontent.com/u/46349796?v=4)](https://github.com/MatthewPageUK "MatthewPageUK (20 commits)")

---

Tags

laravelMatthew Pagelaravel-bitty-enums

###  Code Quality

TestsPest

Code StyleLaravel Pint

### Embed Badge

![Health badge](/badges/matthewpageuk-laravel-bitty-enums/health.svg)

```
[![Health](https://phpackages.com/badges/matthewpageuk-laravel-bitty-enums/health.svg)](https://phpackages.com/packages/matthewpageuk-laravel-bitty-enums)
```

###  Alternatives

[spatie/laravel-prometheus

Export Laravel metrics to Prometheus

2651.3M6](/packages/spatie-laravel-prometheus)[vormkracht10/laravel-mails

Laravel Mails can collect everything you might want to track about the mails that has been sent by your Laravel app.

24149.7k](/packages/vormkracht10-laravel-mails)[bilfeldt/laravel-request-logger

Log Laravel application request and responses for debugging or statistics

122163.3k2](/packages/bilfeldt-laravel-request-logger)[api-platform/laravel

API Platform support for Laravel

59126.4k6](/packages/api-platform-laravel)[ralphjsmit/laravel-helpers

A package containing handy helpers for your Laravel-application.

13704.6k2](/packages/ralphjsmit-laravel-helpers)[basillangevin/laravel-data-json-schemas

Transforms Spatie Data objects into JSON Schemas with built-in validation

1312.2k1](/packages/basillangevin-laravel-data-json-schemas)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
