1. Go to this page and download the library: Download cmandersen/bitwise library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
cmandersen / bitwise example snippets
// In your migration
Schema::create('users', function (Blueprint $table) {
$table->id();
$table->string('name');
$table->unsignedInteger('permissions')->default(0);
$table->unsignedInteger('features')->default(0);
$table->timestamps();
});
use Illuminate\Database\Eloquent\Model;
use Cmandersen\Bitwise\AsBitwise;
class User extends Model
{
protected function casts(): array
{
return [
// Method 1: Auto-generate flags from names
'permissions' => AsBitwise::auto(['read', 'write', 'delete', 'admin']),
// Method 2: Define flags with explicit values
'features' => AsBitwise::flags(['notifications' => 1, 'dark_mode' => 2, 'beta' => 4]),
// Method 3: Shorthand syntax
'roles' => 'bitwise:user,moderator,admin',
'settings' => 'bitwise:email_notifications,push_notifications,sms_notifications',
];
}
}
$user = User::find(1);
// Check if flags are set
if ($user->permissions->has('read')) {
// User has read permission
}
if ($user->permissions->has('read', 'write')) {
// User has both read AND write permissions
}
if ($user->permissions->hasAny('read', 'admin')) {
// User has read OR admin permissions (or both)
}
// Add flags
$user->permissions = $user->permissions->add('write');
$user->permissions = $user->permissions->add('delete', 'admin');
// Remove flags
$user->permissions = $user->permissions->remove('delete');
// Toggle flags
$user->permissions = $user->permissions->toggle('admin');
// Get only specific flags
$readWrite = $user->permissions->only('read', 'write');
// Get all except specific flags
$withoutAdmin = $user->permissions->except('admin');
// Clear all flags
$user->permissions = $user->permissions->clear();
// Set all available flags
$user->permissions = $user->permissions->all();
// Save changes
$user->save();
// Find users who have 'read' permission
$readUsers = User::whereBitwise('permissions', 'read')->get();
// Find users who have both 'read' AND 'write' permissions
$readWriteUsers = User::whereBitwise('permissions', ['read', 'write'])->get();
// Find users who have ANY of the specified permissions
$moderatorUsers = User::whereHasAnyBitwise('permissions', ['write', 'delete'])->get();
// Find users who DON'T have admin permissions
$regularUsers = User::whereDoesntHaveBitwise('permissions', 'admin')->get();
// Find users with EXACTLY the 'read' permission (no other permissions)
$readOnlyUsers = User::whereBitwiseEquals('permissions', 'read')->get();
// Check if flag is set (read-only)
if ($user->permissions['read']) {
// User has read permission
}
// Note: Direct assignment is not allowed - use add()/remove() methods instead
// $user->permissions['read'] = true; // This will throw an exception
// Using array of flag names
User::create([
'name' => 'John Doe',
'permissions' => ['read', 'write'],
]);
// Using a BitwiseCollection
$permissions = (new BitwiseCollection(0, ['read' => 1, 'write' => 2]))
->add('read', 'write');
User::create([
'name' => 'Jane Doe',
'permissions' => $permissions,
]);
// Using direct integer value (if you know the bit values)
User::create([
'name' => 'Admin User',
'permissions' => 15, // 1 + 2 + 4 + 8 = all flags
]);