Laravel 获取所有关系的状态为 2 的模型集合
Laravel get collection of models where all relations has status = 2
我有一个具有子类别关系的类别模型。我需要获取所有子类别都处于活动状态的所有类别。
请帮我形成一个查询
我做到了
$query->whereHas('subcategory', function($query) {
query->where('subcategory.status', '=', 'passed');
});
但是这个查询returns类目中至少有一个子类目是活跃的,但我需要所有子类目都搁浅,状态是活跃的
我相信您可以通过子查询比较传递的子查询数与总数来做到这一点。必须采用命名约定,但您可以调整它以适合您的表格。
$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();
我有一个具有子类别关系的类别模型。我需要获取所有子类别都处于活动状态的所有类别。 请帮我形成一个查询
我做到了
$query->whereHas('subcategory', function($query) {
query->where('subcategory.status', '=', 'passed');
});
但是这个查询returns类目中至少有一个子类目是活跃的,但我需要所有子类目都搁浅,状态是活跃的
我相信您可以通过子查询比较传递的子查询数与总数来做到这一点。必须采用命名约定,但您可以调整它以适合您的表格。
$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();