1. Go to this page and download the library: Download zuko/laravel-bit-masks 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/ */
zuko / laravel-bit-masks example snippets
// app/BitMasks/Network.php
enum Network: int
{
use BitMaskFlags;
case Gmail = 1 << 0;
case Yahoo = 1 << 1;
case Outlook = 1 << 2;
case Hotmail = 1 << 3;
}
use Zuko\BitMasks\Concerns\HasBitMasks;
class Subscriber extends Model
{
use HasBitMasks;
protected $bitMasks = [
'networks' => Network::class, // bound to a flag enum
// 'toggles', // or a plain mask column
];
}
final class Network
{
public const GMAIL = 1 << 0;
public const YAHOO = 1 << 1;
}
$table->bitMask('networks'); // = $table->unsignedBigInteger('networks')->default(0)
$table->wideBitMask('networks', 2); // networks_1, networks_2 — two BIGINTs, default 0 (see Wide masks)
$table->flagPivot('email', 'network_id'); // junction table: flag_id column + composite PK + reverse index (see Junction masks)
enum Network: int
{
case Gmail = 0; // column 1, bit 0
case Yahoo = 1;
// …
case Proton = 63; // column 2, bit 0 (the 64th flag)
case Icloud = 125; // column 2, bit 62 (the 126th flag)
}
class Email extends Model
{
use HasBitMasks;
protected $bitMasks = [
// 'columns' may be a count (derives networks_1..N) or an explicit list.
'networks' => ['columns' => 2, 'enum' => Network::class],
];
}
$email->networks = [Network::Gmail, Network::Proton]; // fans out to networks_1 & networks_2
$email->networks; // WideBitMask instance
$email->networks->names(); // ['Gmail', 'Proton']
$email->hasMask('networks', Network::Icloud); // false
$email->addMask('networks', Network::Icloud)->save();
Email::whereMaskHas('networks', Network::Icloud)->get(); // matched in the right column
Email::whereMaskHasAny('networks', [Network::Gmail, Network::Proton])->get(); // OR across columns
enum Network: int
{
case Gmail = 1;
case Proton = 100;
case Icloud = 250;
}
Schema::create('email_networks', function (Blueprint $table) {
$table->string('email'); // owner column (its type is yours)
$table->flagPivot('email', 'network_id'); // + network_id, PK & reverse index
});
class Email extends Model
{
use HasBitMasks;
protected $bitMasks = [
'networks' => [
'pivot' => 'email_networks',
'enum' => Network::class,
'foreignPivotKey' => 'email', // owner column in the junction table
'flagKey' => 'network_id',
'ownerKey' => 'email', // local key it references
],
];
}