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.
Informations about the package laravel-bitwise-trait
Laravel Bitwise Trait
A simple trait to use bitwise operators on any class Inspired by http://php.net/manual/de/language.operators.bitwise.php#108679
Updated after reading this blog post: https://aaronfrancis.com/2021/bitmasking-in-laravel-and-mysql
I just used it in Laravel so far, but you should be able to use it anywhere else with minor modifications.
PHP Version
Version v2. requires PHP 7.4+. If you're stuck to an older version, please use v1.
Installation
You can install the package via composer:
That's all, no provider registration needed :)
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):
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:
The best way to define your properties is via constants directly in the model. You're of course free to use config variables or whatever you prefer.
This alternative syntax may be easier to read:
Or directly in binary notation:
To set a property, just call the function like this:
To get a property, just call the function like this:
The first parameter ('status' in the example) is always the column you set in the database. Maybe you want to define that in a constant or variable.
To make your life easier, I recommend to use custom getters and setters.
Scopes
If you want to use the new field in scopes, you can do that like this: