PHP code example of aw-studio / laravel-bitflags

1. Go to this page and download the library: Download aw-studio/laravel-bitflags 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/ */

    

aw-studio / laravel-bitflags example snippets


class Email extends Model
{
    // Email status flags, all powers of 2
    public const SENT = 1;
    public const RECEIVED = 2;
    public const SEEN = 4;
    public const READ = 8;

    protected $fillable = ['status'];

    public $casts = [
        'status' => Bitflags::class
    ];
}

public function markRead()
{
    $this->update([
        'status' => addBitflag(self::READ, $this->status)
    ]);
}

$this->update([
    'status' => addBitflag([self::READ, self::SEEN], $this->status)
]);

public function markUnread()
{
    $this->update([
        'status' => removeBitflag(self::READ, $this->status)
    ]);
}

$this->update([
    'status' => removeBitflag([self::READ, self::SEEN], $this->status)
]);

public function scopeRead($query)
{
    return $this->whereBitflag('status', self::READ);
}
public function scopeUnread($query)
{
    return $this->whereBitflagNot('status', self::READ);
}
public function scopeSeenOrRead($query)
{
    return $this->whereBitflagIn('status', [self::READ, self::SEEN]);
}
public function scopeSeenAndRead($query)
{
    return $this->whereBitflags('status', [self::READ, self::SEEN]);
}

protected $appends = ['read'];

public function getReadAttribute()
{
    return inBitmask(self::READ, $this->status);
}