PHPackages                             fanmade/laravel-bitwise-trait - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. fanmade/laravel-bitwise-trait

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

fanmade/laravel-bitwise-trait
=============================

Simple trait to use bitwise operators on any class

2.0.0(4y ago)1376.5k↓16.3%2[1 PRs](https://github.com/Fanmade/laravel-bitwise-trait/pulls)MITPHPPHP &gt;=7.4CI passing

Since May 28Pushed 1mo ago1 watchersCompare

[ Source](https://github.com/Fanmade/laravel-bitwise-trait)[ Packagist](https://packagist.org/packages/fanmade/laravel-bitwise-trait)[ Docs](https://github.com/Fanmade/laravel-bitwise-trait)[ RSS](/packages/fanmade-laravel-bitwise-trait/feed)WikiDiscussions master Synced 2w ago

READMEChangelog (4)Dependencies (1)Versions (6)Used By (0)

Laravel Bitwise Trait
=====================

[](#laravel-bitwise-trait)

[![Latest Version on Packagist](https://camo.githubusercontent.com/895bd866c4e34278a48760051b4113b08f74ee5e9d2dedb83723c20f94f557b6/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f66616e6d6164652f6c61726176656c2d626974776973652d74726169742e737667)](https://packagist.org/packages/fanmade/laravel-bitwise-trait)[![Tests](https://github.com/Fanmade/laravel-bitwise-trait/actions/workflows/tests.yml/badge.svg)](https://github.com/Fanmade/laravel-bitwise-trait/actions/workflows/tests.yml)[![Total Downloads](https://camo.githubusercontent.com/aac7cc6f3a0dd3acfa9cc67a83c0eb1282919ce69345b2274fa86927d42c424d/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f66616e6d6164652f6c61726176656c2d626974776973652d74726169742e737667)](https://packagist.org/packages/fanmade/laravel-bitwise-trait)[![License](https://camo.githubusercontent.com/e03ed31578dc830ffca93277bd1afaba6c5cd14d542c96cf33c1a750762f0476/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f6c2f66616e6d6164652f6c61726176656c2d626974776973652d74726169742e737667)](LICENSE.md)

A tiny, framework-agnostic trait for storing many boolean flags in a **single integer column** using bitwise operations. One `tinyInteger` column can hold up to 8 independent true/false values instead of 8 separate columns.

Although it's built with Laravel in mind, the trait itself has **no framework dependency** — it works in any PHP class.

Inspired by the [PHP manual on bitwise operators](https://www.php.net/manual/en/language.operators.bitwise.php#108679) and Aaron Francis' write-up on [bitmasking in Laravel and MySQL](https://aaronfrancis.com/2021/bitmasking-in-laravel-and-mysql).

Requirements
------------

[](#requirements)

- PHP **8.2+**

> Using an older stack? `v2.*` supports PHP 7.4+, and `v1.*` supports even older versions. See the [upgrade notes](CHANGELOG.md) before moving to `v3`.

Installation
------------

[](#installation)

```
composer require fanmade/laravel-bitwise-trait
```

No service provider registration is needed.

How it works
------------

[](#how-it-works)

Each flag is a single bit, so every flag's value is a distinct power of two (`1, 2, 4, 8, …`). A record's integer column is the bitwise OR of all the flags that are currently set. Because every flag owns its own bit, they never collide — you can read, set, or toggle one without touching the others.

You need one (ideally **unsigned**) integer column to store the flags. Pick its size based on how many flags you need — you only need one bit per flag:

```
$table->unsignedTinyInteger('status');   // 1 byte  -> up to 8 flags
$table->unsignedSmallInteger('status');  // 2 bytes -> up to 16 flags
$table->unsignedMediumInteger('status'); // 3 bytes -> up to 24 flags
$table->unsignedInteger('status');       // 4 bytes -> up to 32 flags
```

Most of the time an `unsignedTinyInteger` is all you need. You can add as many flag columns as you like if you outgrow a single one.

Quick start
-----------

[](#quick-start)

Add the trait to your model and define one flag constant per bit. The clearest way to write the values is the shift syntax `1
