一键导入
bitmask-development
Work with bitmask flags in Laravel using the Transistor package, including creating enums, model setup, querying, and validation.
用 Codex 或 Claude 帮你安装 复制这段 Prompt,粘贴到 Codex、Claude 或其他助手里,让它检查 Skill 页面并帮你完成安装。
菜单
Work with bitmask flags in Laravel using the Transistor package, including creating enums, model setup, querying, and validation.
用 Codex 或 Claude 帮你安装 复制这段 Prompt,粘贴到 Codex、Claude 或其他助手里,让它检查 Skill 页面并帮你完成安装。
基于 SOC 职业分类
| name | bitmask-development |
| description | Work with bitmask flags in Laravel using the Transistor package, including creating enums, model setup, querying, and validation. |
Activate when:
Use php artisan make:bitmask-flags to generate enums interactively. The command will prompt for flag names.
Flags use bit-shifted values:
1 << 0 (1)1 << 1 (2)1 << 2 (4)1 << 3 (8)Example enum:
enum Permission: int
{
case Read = 1 << 0; // 1
case Write = 1 << 1; // 2
case Delete = 1 << 2; // 4
case Admin = 1 << 3; // 8
}
Add the trait and cast to your model:
use Gksh\Transistor\Casts\BitmaskCast;
use Gksh\Transistor\Concerns\HasBitmaskScopes;
class User extends Model
{
use HasBitmaskScopes;
protected function casts(): array
{
return [
'permissions' => BitmaskCast::class,
// Or specify size: BitmaskCast::class.':tiny'
];
}
}
Cast size options: tiny, small, medium, or default (32-bit).
Schema::create('users', function (Blueprint $table) {
$table->id();
$table->bitmask('permissions'); // 32-bit unsigned integer, default 0
$table->tinyBitmask('settings'); // 8-bit (up to 8 flags)
$table->smallBitmask('preferences'); // 16-bit (up to 16 flags)
$table->mediumBitmask('features'); // 24-bit (up to 24 flags)
});
Filter records by bitmask flags:
// Has a specific flag
User::whereHasBitmaskFlag('permissions', Permission::Admin)->get();
// Has ALL specified flags
User::whereHasAllBitmaskFlags('permissions', [Permission::Read, Permission::Write])->get();
// Has ANY of the specified flags
User::whereHasAnyBitmaskFlag('permissions', [Permission::Admin, Permission::Delete])->get();
// Does NOT have a specific flag
User::whereDoesntHaveBitmaskFlag('permissions', Permission::Admin)->get();
// Does NOT have ANY of the specified flags
User::whereDoesntHaveAnyBitmaskFlag('permissions', [Permission::Admin, Permission::Delete])->get();
In a Form Request:
use Gksh\Transistor\Rules\ValidBitmask;
public function rules(): array
{
return [
// Basic validation (any non-negative integer)
'permissions' => ['required', new ValidBitmask],
// With enum constraint (validates against max possible value)
'permissions' => ['required', new ValidBitmask(Permission::class)],
// Using inline rule syntax
'permissions' => ['required', 'bitmask:'.Permission::class],
];
}
Check flags in Blade templates:
@hasBitmaskFlag($user->permissions, Permission::Admin)
<p>User is an admin</p>
@endhasBitmaskFlag
@hasAnyBitmaskFlag($user->permissions, [Permission::Read, Permission::Write])
<p>User can read or write</p>
@endhasAnyBitmaskFlag
@hasAllBitmaskFlags($user->permissions, [Permission::Read, Permission::Write])
<p>User can read AND write</p>
@endhasAllBitmaskFlags
Use the inspect command to visualize flags:
# Show all flags for an enum
php artisan bitmask:inspect Permission
# Analyze a specific value
php artisan bitmask:inspect Permission 7
This displays a table with case names, decimal values, and binary representation with colored bits.
The cast returns a Gksh\Bitmask\Bitmask object:
$user = User::find(1);
// Check if a flag is set
$user->permissions->has(Permission::Admin);
// Get the integer value
$user->permissions->value();