Download the PHP package fanmade/laravel-bitwise-trait without Composer

On this page you can find all versions of the php package fanmade/laravel-bitwise-trait. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.

FAQ

After the download, you have to make one include require_once('vendor/autoload.php');. After that you have to import the classes with use statements.

Example:
If you use only one package a project is not needed. But if you use more then one package, without a project it is not possible to import the classes with use statements.

In general, it is recommended to use always a project to download your libraries. In an application normally there is more than one library needed.
Some PHP packages are not free to download and because of that hosted in private repositories. In this case some credentials are needed to access such packages. Please use the auth.json textarea to insert credentials, if a package is coming from a private repository. You can look here for more information.

  • Some hosting areas are not accessible by a terminal or SSH. Then it is not possible to use Composer.
  • To use Composer is sometimes complicated. Especially for beginners.
  • Composer needs much resources. Sometimes they are not available on a simple webspace.
  • If you are using private repositories you don't need to share your credentials. You can set up everything on our site and then you provide a simple download link to your team member.
  • Simplify your Composer build process. Use our own command line tool to download the vendor folder as binary. This makes your build process faster and you don't need to expose your credentials for private repositories.
Please rate this library. Is it a good library?

Informations about the package laravel-bitwise-trait

Laravel Bitwise Trait

Latest Version on Packagist Tests Total Downloads

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 and Aaron Francis' write-up on bitmasking in Laravel and MySQL.

Requirements

Using an older stack? v2.* supports PHP 7.4+, and v1.* supports even older versions. See the upgrade notes before moving to v3.

Installation

No service provider registration is needed.

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:

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

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 << n:

The trait gives you three protected helper methods to call from inside your model. The first argument is always the name of the column that stores the flags:

Because they're protected, you expose each flag through a small accessor (below) rather than calling them from outside the model.

Exposing flags as attributes

Wrap each flag in an accessor/mutator so it reads and writes like a normal boolean attribute.

Since Laravel 9 the idiomatic way is a single Attribute method per flag:

Now the flag behaves like any other attribute:

Older Laravel (magic-method accessors) Before Laravel 9 you'd define the accessor and mutator as separate magic methods:

Querying by flag (scopes)

To filter records by a flag, add a query scope. A bit is set when column & flag = flag, and unset when column & flag = 0.

Since Laravel 12 you can use the #[Scope] attribute, which drops the old scope method-name prefix:

Older Laravel (scope-prefixed methods) Before Laravel 12, name the method with a `scope` prefix instead of using the attribute:

Writing flag values

Beyond the shift syntax, you can define the constants however reads best to you — all three of these are equivalent:

Testing

Credits

License

The MIT License (MIT). See License File for more information.


All versions of laravel-bitwise-trait with dependencies

PHP Build Version
Package Version
Requires php Version >=7.4
Composer command for our command line client (download client) This client runs in each environment. You don't need a specific PHP version etc. The first 20 API calls are free. Standard composer command

The package fanmade/laravel-bitwise-trait contains the following files

Loading the files please wait ...