belongsTo('App\Models\ProductCategory', 'parent_id', 'id'); } public function subCategories() { return $this->hasMany($this, 'parent_id', 'id')->orderBy('order', 'asc'); } public function filters() { return $this->hasMany('App\Models\ProductFilter', 'category_id', 'id'); } public function products() { return $this->hasMany('App\Models\Product', 'category_id', 'id'); } public function getUrl() { return '/products?category_id=' . $this->id; } public function getSelfAndChideProductsCount($productType = null) { $ids = [$this->id]; $subCategoriesIds = $this->subCategories->pluck('id')->toArray(); $ids = array_merge($ids, $subCategoriesIds); $query = Product::whereIn('category_id', $ids); if (!empty($productType)) { $query->where('type', $productType); } return $query->count(); } }