Skip to content

Conversation

mohsenetm
Copy link
Contributor

Summary

This PR introduces native support for bitwise operations in Laravel's Query Builder and Eloquent ORM. Currently, developers need to use whereRaw() for bitwise operations, which reduces code readability and type safety.

What does this PR do?

  1. Adds new Query Builder methods:

    • whereBitwise() / orWhereBitwise() - Check if bits are set
    • whereNotBitwise() / orWhereNotBitwise() - Check if bits are not set
    • whereBitwiseExact() - Check for exact bit pattern match
  2. Adds Eloquent Builder support:

    • All Query Builder bitwise methods available in Eloquent
    • Maintains consistency with existing Eloquent patterns

Breaking Changes

None. This PR only adds new functionality without modifying existing behavior.

Usage Examples

Basic Usage:

// Check if user has read permission (bit 4)
User::whereBitwise('permissions', 4)->get();

// Check multiple permissions with OR
User::whereBitwise('permissions', 4)
    ->orWhereBitwise('permissions', 2)
    ->get();

// Check users without delete permission
User::whereNotBitwise('permissions', 8)->get();

// Check exact permission set (all CRUD = 15)
User::whereBitwiseExact('permissions', 15, 15)->get();

Complex Queries:

User::where(function ($query) {
    $query->whereBitwise('permissions', 4)
          ->orWhereBitwise('permissions', 2);
})->whereNotBitwise('permissions', 8)->get();

[12.x] Add native bitwise operator support to Query Builder and Eloquent

fix: fix style ci
@mohsenetm
Copy link
Contributor Author

The tests have encountered failures that are unrelated to the code I’ve written. I have been working on the Query Builder and Eloquent Builder, whereas the errors are related to a different part of the framework.

@shaedrich
Copy link
Contributor

Shouldn't this rather be called "bitmask"?

@taylorotwell
Copy link
Member

Thanks for your pull request to Laravel!

Unfortunately, I'm going to delay merging this code for now. To preserve our ability to adequately maintain the framework, we need to be very careful regarding the amount of code we include.

If applicable, please consider releasing your code as a package so that the community can still take advantage of your contributions!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants