code: def apply_scopes(self):
          
          if not self._scopes:
              return self
  
          builder = copy.copy(self)
  
          query = builder.get_query()
  
          # We will keep track of how many wheres are on the query before running the
          # scope so that we can properly group the added scope constraints in the
          # query as their own isolated nested where statement and avoid issues.
          original_where_count = len(query.wheres)
  
          where_counts = [0, original_where_count]
  
          for scope in self._scopes.values():
              self._apply_scope(scope, builder)
  
              # Again, we will keep track of the count each time we add where clauses so that
              # we will properly isolate each set of scope constraints inside of their own
              # nested where clause to avoid any conflicts or issues with logical order.
              where_counts.append(len(query.wheres))
  
          if self._should_nest_wheres_for_scope(query, original_where_count):
              self._nest_wheres_for_scope(query, Collection(where_counts).unique().all())
  
          return builder
docs: Get the underlying query builder instance with applied global scopes.
  
          :type: Builder