PHPackages                             degecko/laravel-eloquent-bitwise - 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. [Database &amp; ORM](/categories/database)
4. /
5. degecko/laravel-eloquent-bitwise

ActiveLibrary[Database &amp; ORM](/categories/database)

degecko/laravel-eloquent-bitwise
================================

Manage boolean flags and statuses as a fluent collection on Eloquent models

1.1.0(1mo ago)00MITPHPPHP ^8.2

Since Apr 13Pushed 1mo agoCompare

[ Source](https://github.com/degecko/laravel-eloquent-bitwise)[ Packagist](https://packagist.org/packages/degecko/laravel-eloquent-bitwise)[ RSS](/packages/degecko-laravel-eloquent-bitwise/feed)WikiDiscussions main Synced 1w ago

READMEChangelogDependencies (4)Versions (2)Used By (0)

Laravel Eloquent Bitwise
========================

[](#laravel-eloquent-bitwise)

Store multiple boolean flags in a single integer column using bitwise operations. Provides a fluent API for reading, modifying, and querying flags on Eloquent models.

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

[](#installation)

```
composer require degecko/laravel-eloquent-bitwise
```

Setup
-----

[](#setup)

Add an integer column to your migration:

```
$table->integer('status')->default(0);
$table->smallInteger('permissions')->default(0);
```

Use the trait in your model and list your flags:

```
use DeGecko\Bitwise\HasBitwiseFlags;

class User extends Model
{
    use HasBitwiseFlags;

    public array $bitwiseCasts = [
        'status' => ['active', 'verified', 'premium', 'suspended'],
        'permissions' => ['read', 'write', 'execute'],
    ];
}
```

That's it. The trait automatically registers the casts and assigns each flag a bit value based on its position (1, 2, 4, 8, ...).

### Important: Flag Ordering

[](#important-flag-ordering)

**Do not reorder, rename, or remove flags from the middle of the array.** The bit values are determined by position. Changing the order will corrupt existing data in the database.

Always **append new flags to the end** of the array:

```
// Before
'status' => ['active', 'verified', 'premium'],

// Correct — append to the end
'status' => ['active', 'verified', 'premium', 'suspended'],

// WRONG — do NOT insert or reorder
'status' => ['suspended', 'active', 'verified', 'premium'],
```

If you need to deprecate a flag, leave it in place and stop using it in your code, or switch to explicit values:

```
public array $bitwiseCasts = [
    'status' => [
        'active'    => 1,
        'verified'  => 2,
        // 'removed' was 4 — gap is intentional
        'premium'   => 8,
        'suspended' => 16,
    ],
];
```

Usage
-----

[](#usage)

### Checking flags

[](#checking-flags)

```
$user->status->is('verified');             // true if 'verified' is set
$user->status->is('admin', 'moderator');   // true if either is set
$user->status->either('vip', 'premium');   // alias for is()

$user->status->not('suspended');           // true if 'suspended' is not set
$user->status->not('suspended', 'banned'); // true if ALL are absent
$user->status->neither('suspended', 'banned'); // true if NONE are set
```

### Modifying flags

[](#modifying-flags)

```
$user->status->set('verified');              // add a flag
$user->status->set('premium', 'active');     // add multiple flags
$user->status->remove('suspended');          // remove a flag
$user->status->toggle('active');             // flip on/off
$user->status->toggle('active', true);       // explicitly set on
$user->status->toggle('active', false);      // explicitly set off
```

### Persisting

[](#persisting)

```
// Chain modifications and save in one go
$user->status->set('verified')->remove('pending')->save();

// Or save the model directly
$user->status->set('verified');
$user->save();
```

### Querying

[](#querying)

```
// Find users where specific flags are set
User::bitwise('status', 'active', 'verified')->get();

// Find users where specific flags are NOT set
User::bitwiseNot('status', 'suspended', 'banned')->get();

// Combine with other query conditions
User::bitwise('status', 'active')
    ->bitwiseNot('status', 'suspended')
    ->where('created_at', '>', now()->subDays(30))
    ->get();
```

How it works
------------

[](#how-it-works)

Flags are stored as a single integer using bitwise operations. Each flag is assigned a power of 2 (1, 2, 4, 8, ...), allowing up to 32 or 64 flags per column depending on your integer size.

For example, a user with `active` (1) and `premium` (4) flags has a stored value of `5` (`1 | 4`). Checking for a flag uses bitwise AND: `5 & 4` is truthy, so `premium` is set.

This is far more storage-efficient than JSON arrays or separate boolean columns, and queries use simple integer math that databases handle natively.

API Reference
-------------

[](#api-reference)

MethodReturnsDescription`is(...$flags)``bool`True if any flag is set`either(...$flags)``bool`Alias for `is()``not(...$flags)``bool`True if all flags are absent`neither(...$flags)``bool`True if none of the flags are set`set(...$flags)``self`Add flags (idempotent)`remove($flag)``self`Remove a flag`toggle($flag, ?bool)``self`Flip or explicitly set a flag`save()``bool`Persist the parent model### Query Scopes

[](#query-scopes)

ScopeDescription`bitwise($column, ...$flags)`Where all given flags are set`bitwiseNot($column, ...$flags)`Where all given flags are not setLicense
-------

[](#license)

MIT

###  Health Score

37

—

LowBetter than 81% of packages

Maintenance89

Actively maintained with recent releases

Popularity0

Limited adoption so far

Community6

Small or concentrated contributor base

Maturity46

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

Unknown

Total

1

Last Release

57d ago

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/5130992?v=4)[Cosmin Gheorghita](/maintainers/degecko)[@degecko](https://github.com/degecko)

---

Top Contributors

[![degecko](https://avatars.githubusercontent.com/u/5130992?v=4)](https://github.com/degecko "degecko (1 commits)")

---

Tags

laraveleloquentflagscollectionstatusbitwise

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/degecko-laravel-eloquent-bitwise/health.svg)

```
[![Health](https://phpackages.com/badges/degecko-laravel-eloquent-bitwise/health.svg)](https://phpackages.com/packages/degecko-laravel-eloquent-bitwise)
```

###  Alternatives

[kirschbaum-development/eloquent-power-joins

The Laravel magic applied to joins.

1.6k29.9M42](/packages/kirschbaum-development-eloquent-power-joins)[mongodb/laravel-mongodb

A MongoDB based Eloquent model and Query builder for Laravel

7.1k8.0M84](/packages/mongodb-laravel-mongodb)[spatie/laravel-sluggable

Generate slugs when saving Eloquent models

1.5k12.4M291](/packages/spatie-laravel-sluggable)[psalm/plugin-laravel

Psalm plugin for Laravel

3325.1M337](/packages/psalm-plugin-laravel)[watson/validating

Eloquent model validating trait.

9743.4M53](/packages/watson-validating)[yajra/laravel-oci8

Oracle DB driver for Laravel via OCI8

8733.1M23](/packages/yajra-laravel-oci8)

PHPackages © 2026

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