app/Models/Traits/HasCategoryTrait.php
public function scopeCategory($query, $categoryId = null)
    {
        if ($categoryId) {
            // Get descendants and self inventory category nodes
            $categories = Category::find($categoryId)->getDescendantsAndSelf();

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

        return $query;
    }
public function scopeCategory($query, $categoryId = null)
    {
        if ($categoryId) {
            // Get descendants and self inventory category nodes
            $categories = Category::find($categoryId)->getDescendantsAndSelf();

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

        return $query;
    }
Filters results by specified category.

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

@return mixed