0
votes

I have a category model with a subcategory relationship. I need to get all categories in which all subcategories have the active status. Please help me to form a query

I did

$query->whereHas('subcategory', function($query) {
            query->where('subcategory.status', '=', 'passed');
        });

but this query returns categories in which at least one subcategory is active, but I need all subcategories to be stranded, the status is active

1
post your model we can check the relationshipsBasharmal
public function subcategory() { return $this->hasMany(CategorySubcategory::class, 'category_id'); }Юлий Реут

1 Answers

0
votes

I would believe you would be able to do it with sub queries comparing the count of sub queries passed to the total. Had to asume the naming conventions, but you can tweak it to fit your tables.

$totalSubCategories = SubCategory::selectRaw('Count(subcategories.id) as totalc')
    ->whereColumn('categories.id', 'subcategories.parent_id');

$totalPassedSubCategories = SubCategory::selectRaw('Count(subcategories.id) as totalpassed')
    ->whereColumn('categories.id', 'subcategories.parent_id')
    ->where('status', 'passed');

Category::where($totalSubCategories, $totalPassedSubCategories)->get();