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)1575.0k↓17.7%2[1 PRs](https://github.com/Fanmade/laravel-bitwise-trait/pulls)MITPHPPHP &gt;=7.4CI failing

Since May 28Pushed 5mo 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 2d ago

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

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

[](#laravel-bitwise-trait)

A simple trait to use bitwise operators on any class Inspired by

Updated after reading this blog post:

I just used it in Laravel so far, but you should be able to use it anywhere else with minor modifications.

PHP Version
-----------

[](#php-version)

Version v2.\* requires PHP 7.4+. If you're stuck to an older version, please use v1.\*

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

[](#installation)

You can install the package via composer:

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

That's all, no provider registration needed :)

Usage
-----

[](#usage)

*This example is for a default Laravel (5.4) Model within the "App" namespace.*

You need an (ideally unsigned) integer field in your database which will store the properties. The length does depend on the number of values you would like to store. You only need one bit per value, so it's 8 values for each byte, if the column is unsigned.

Examples (based on laravel migrations):

```
$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
```

You get the idea. Most times you probably only need an unsigned tinyInteger :)

There are only a few use-cases for more than one database field, but you can add as many fields as you like.

Include the Trait in your model like this:

```
