app/Models/Traits/HasLocationTrait.php
public function scopeLocation($query, $locationId = null)
    {
        if (!is_null($locationId)) {
            // Get descendants and self inventory category nodes
            $locations = Location::find($locationId)->getDescendantsAndSelf();

            // Perform a sub-query on main query
            $query->where(function ($query) use ($locations) {
                // For each category, apply a orWhere query to the sub-query
                foreach ($locations as $location) {
                    $query->orWhere('location_id', $location->id);
                }
            });
        }

        return $query;
    }
public function scopeLocation($query, $locationId = null)
    {
        if (!is_null($locationId)) {
            // Get descendants and self inventory category nodes
            $locations = Location::find($locationId)->getDescendantsAndSelf();

            // Perform a sub-query on main query
            $query->where(function ($query) use ($locations) {
                // For each category, apply a orWhere query to the sub-query
                foreach ($locations as $location) {
                    $query->orWhere('location_id', $location->id);
                }
            });
        }

        return $query;
    }
Filters inventory results by specified location.

@param mixed      $query
@param int|string $locationId

@return mixed