Eloquent "Query Builder" with "OR" operator neutralizes my Global Scope - Laravel

Well, it seems that your solution to add parentheses is the best workaround, but I have a suggestion how to do that slightly better way.

  1. Create new class QueryBuilder. For example, in \App\Models\ namespace (app/Models/ folder):

    namespace App\Models;
    
    use Illuminate\Database\Query\Builder as EloquentQueryBuilder;
    
    class QueryBuilder extends EloquentQueryBuilder {
    
      public function whereRaw($sql, $bindings = [], $boolean = 'and')
      {
        return parent::whereRaw('('.$sql.')', $bindings, $boolean);
      }
    
    }
    
  2. Add this code to your BaseEloquentModel class:

    use Illuminate\Database\Eloquent\Model;
    use App\Models\QueryBuilder; // <-- addition
    
    class BaseEloquentModel extends Model {
      // ...
      protected function newBaseQueryBuilder()
      {
        $connection = $this->getConnection();
    
        return new QueryBuilder(
            $connection,
            $connection->getQueryGrammar(),
            $connection->getPostProcessor()
        );
      }
      // ...
    }
    

Now, all whereRaw() calls will automatically have parentheses around query.