degecko/laravel-eloquent-bitwise
Composer 安装命令:
composer require degecko/laravel-eloquent-bitwise
包简介
Manage boolean flags and statuses as a fluent collection on Eloquent models
README 文档
README
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
composer require degecko/laravel-eloquent-bitwise
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
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
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
$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
// 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
// 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
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
| Method | Returns | Description |
|---|---|---|
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
| Scope | Description |
|---|---|
bitwise($column, ...$flags) |
Where all given flags are set |
bitwiseNot($column, ...$flags) |
Where all given flags are not set |
License
MIT
degecko/laravel-eloquent-bitwise 适用场景与选型建议
degecko/laravel-eloquent-bitwise 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 0 次下载、GitHub Stars 达 0, 最近一次更新时间为 2026 年 04 月 13 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「flags」 「laravel」 「collection」 「eloquent」 「status」 「bitwise」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 degecko/laravel-eloquent-bitwise 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 degecko/laravel-eloquent-bitwise 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 degecko/laravel-eloquent-bitwise 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
This package provides type-safe extension of the laravel collection, forcing a single type of object.
ZF2 module for the Opensoft Rollout library
A PHP trait to enable bitwise comparison of flags
Traits to build collections of specific objects
Adds flags support to your model/entity
ItalyStrap Helpers
统计信息
- 总下载量: 0
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 32
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2026-04-13