PHPackages                             alazzi-az/laravel-bitmask - 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. alazzi-az/laravel-bitmask

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

alazzi-az/laravel-bitmask
=========================

laravel package to work with bitmasking

v1.0.4(1y ago)9649↓50%2[1 issues](https://github.com/alazzi-az/laravel-bitmask/issues)[3 PRs](https://github.com/alazzi-az/laravel-bitmask/pulls)MITPHPPHP ^8.2CI passing

Since Oct 12Pushed 1mo agoCompare

[ Source](https://github.com/alazzi-az/laravel-bitmask)[ Packagist](https://packagist.org/packages/alazzi-az/laravel-bitmask)[ Docs](https://github.com/alazzi-az/laravel-bitmask)[ GitHub Sponsors](https://github.com/mohammedazman)[ RSS](/packages/alazzi-az-laravel-bitmask/feed)WikiDiscussions main Synced 1mo ago

READMEChangelog (5)Dependencies (13)Versions (9)Used By (0)

Laravel Bitmask
===============

[](#laravel-bitmask)

[![Latest Version on Packagist](https://camo.githubusercontent.com/62de51a1e36eb43b58d275fe6bb7e25ab1866407278dd37c1b5e9e1d0a671e1a/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f616c617a7a692d617a2f6c61726176656c2d6269746d61736b2e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/alazzi-az/laravel-bitmask)[![GitHub Tests Action Status](https://camo.githubusercontent.com/93e6c3eed0597787de3476779d4d05d6addb395aca4acd6242cf2acc586e154c/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f616374696f6e732f776f726b666c6f772f7374617475732f616c617a7a692d617a2f6c61726176656c2d6269746d61736b2f72756e2d74657374732e796d6c3f6272616e63683d6d61696e266c6162656c3d7465737473)](https://github.com/alazzi-az/laravel-bitmask/actions?query=workflow%3Arun-tests+branch%3Amain)[![GitHub Code Style Action Status](https://camo.githubusercontent.com/f1ebb3d088f067bcae4f783d3e229fbfcd643d8de969ca61dfb200aa83df7721/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f616374696f6e732f776f726b666c6f772f7374617475732f616c617a7a692d617a2f6c61726176656c2d6269746d61736b2f6669782d7068702d636f64652d7374796c652d6973737565732e796d6c3f6272616e63683d6d61696e266c6162656c3d636f64652532307374796c65)](https://github.com/alazzi-az/laravel-bitmask/actions?query=workflow%3A%22Fix+PHP+code+style+issues%22+branch%3Amain)[![Total Downloads](https://camo.githubusercontent.com/98bb8970cb1ad02ba85ebff2b86e863f4d3a46b646927f425efd0c65a66dd935/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f616c617a7a692d617a2f6c61726176656c2d6269746d61736b2e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/alazzi-az/laravel-bitmask)

**Laravel Bitmask** is a powerful wrapper package for integrating the functionality of the [php-bitmask](https://github.com/alazzi-az/php-bitmask) library into Laravel applications. It leverages Laravel's service container and facades to provide an elegant and intuitive interface for reading, validating, and converting bitmasks. The package also simplifies casting bitmasks to and from enum values, enabling developers to efficiently apply bitmasking techniques while harnessing the full power of Laravel’s ecosystem.

Features
--------

[](#features)

- **Bitmask Reading**: Easily retrieve active bits from a given bitmask.
- **Bitmask Validation**: Ensure that provided bits and masks are valid, including checks for single-bit settings.
- **Bitmask Conversion**: Convert indices to bitmasks and vice versa, along with conversions to binary string representations.
- **Casting for Masks and Enums**: Automatically handle the casting of bitmask values to and from enum types, providing a seamless experience when working with enumerated bitmasks.
- **Integration with Laravel**: Utilizes Laravel's facade system for seamless integration and easy access.
- **Eloquent Query Scopes**: Provides a trait with query scopes for performing bitmask operations directly within your Eloquent models.

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

[](#installation)

You can install the package via Composer:

```
composer require alazzi-az/laravel-bitmask
```

Usage
-----

[](#usage)

### 1. HasBitmask Trait

[](#1-hasbitmask-trait)

The `HasBitmask` trait provides Eloquent query scopes for performing bitmask operations on multiple columns within your models.

#### Overview

[](#overview)

The `HasBitmask` trait allows you to:

- **Check if a specific flag is set**.
- **Check if any of a set of flags are set**.
- **Check if all of a set of flags are set**.
- **Check if specific flags are not set**.

#### Integration

[](#integration)

To use the `HasBitmask` trait in your Laravel models, follow these steps:

1. **Import and Use the Trait**

    ```
