1. Go to this page and download the library: Download gksh/transistor 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/ */
gksh / transistor example snippets
// 1. Generate a bitmask enum
php artisan make:bitmask-flags Permission
// 2. Add migration macro
Schema::create('users', function (Blueprint $table) {
$table->id();
$table->bitmask('permissions');
});
// 3. Configure 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,
];
}
}
// 4. Use it
$user->permissions = $user->permissions
->set(Permission::Read)
->set(Permission::Write);
User::whereHasBitmaskFlag('permissions', Permission::Admin)->get();
namespace App\Enums;
enum Permission: int
{
case Read = 1 << 0; // 1
case Write = 1 << 1; // 2
case Delete = 1 << 2; // 4
case Admin = 1 << 3; // 8
}
use Gksh\Transistor\Concerns\HasBitmaskScopes;
class User extends Model
{
use HasBitmaskScopes;
}
// Filter where flag IS set
User::whereHasBitmaskFlag('permissions', Permission::Read)->get();
// Filter where ALL flags are set
User::whereHasAllBitmaskFlags('permissions', [Permission::Read, Permission::Write])->get();
// Filter where ANY flag is set
User::whereHasAnyBitmaskFlag('permissions', [Permission::Write, Permission::Admin])->get();
// Filter where flag is NOT set
User::whereDoesntHaveBitmaskFlag('permissions', Permission::Admin)->get();
// Filter where NONE of the flags are set
User::whereDoesntHaveAnyBitmaskFlag('permissions', [Permission::Read, Permission::Write])->get();