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;
    }