PHP code example of fanmade / laravel-bitwise-trait

1. Go to this page and download the library: Download fanmade/laravel-bitwise-trait 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/ */

    

fanmade / laravel-bitwise-trait example snippets


$table->tinyInteger('status'); // 1 byte -> maximum of 7 different values
$table->unsignedTinyInteger('status'); // maximum of 8 different values
$table->smallInteger('status'); // 2 byte -> maximum of 16 different values
$table->unsignedSmallInteger('status'); // maximum of 17 different values
$table->mediumInteger('status'); // 3 byte -> maximum of 24 different values

 

namespace App;

use Fanmade\Bitwise\BitwiseFlagTrait;

class Message extends Model
{
  use BitwiseFlagTrait;

const MESSAGE_SENT     = 1; // BIT #1 of has the value 1
const MESSAGE_RECEIVED = 2; // BIT #2 of has the value 2
const MESSAGE_SEEN     = 4; // BIT #3 of has the value 4
const MESSAGE_READ     = 8; // BIT #4 of has the value 8

const MESSAGE_SENT     = 1 << 0;
const MESSAGE_RECEIVED = 1 << 1;
const MESSAGE_SEEN     = 1 << 2;
const MESSAGE_READ     = 1 << 3;

const MESSAGE_SENT     = 0b00000001;
const MESSAGE_RECEIVED = 0b00000010;
const MESSAGE_SEEN     = 0b00000100;
const MESSAGE_READ     = 0b00001000;

$this->setFlag('status', self::MESSAGE_SENT, true);

$sent = $this->getFlag('status', self::MESSAGE_SENT);

    public function setSentAttribute($sent = true): self
    {
        $this->setFlag('status', self::MESSAGE_SENT, $sent);
        
        return $this;
    }

    public function getSentAttribute(): bool
    {
        return $this->getFlag('status', self::MESSAGE_SENT);
    }


    /**
     * @param Builder $query
     * @return Builder
     */
    public function scopeUnread($query)
    {
        return $query->whereRAW('NOT status & ' . self::MESSAGE_READ);
    }

    /**
     * @param Builder $query
     * @return Builder
     */
    public function scopeRead($query)
    {
        return $query->where('status', '&', self::MESSAGE_READ);
    }