Answer the question
In order to leave comments, you need to log in
How to get quantity?
There are some categories made according to the nested set algorithm through the . There are also devices associated with many-to-many categories. For clarity
public function devices() {
return $this->belongsToMany("App\Models\Device", "category_device", "category_id", "device_id");
}
public function childrenDevices() {
$categories = $this->descendants()->pluck("id");
return Device::whereHas("categories", function ($query) use ($categories) {
$query->whereIn("category_id", $categories);
});
}
select * from `devices` where exists (select * from `categories` inner join `category_device` on `categories`.`id` = `category_device`.`category_id` where `devices`.`id` = `category_device`.`device_id` and `category_id` in (?, ?, ?) and `categories`.`deleted_at` is null) and `devices`.`deleted_at` is null
SELECT * FROM `category_device` WHERE `category_id` in (3, 4, 5)
Answer the question
In order to leave comments, you need to log in
public function childrenDevices() {
$categories = $this->descendants()->pluck("id");
return Device::whereHas("categories", function ($query) use ($categories) {
$query->whereIn("category_id", $categories);
});
}
SELECT DISTINCT `device_id` FROM `category_device` WHERE `category_id` in (3, 4, 5)
I won’t speak for this particular package, but logically, something like
select
count(*)
from products
inner join categories
on products.category_id = categories .id
and categories.left >= $left and categories.right <= $right
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question