1. Suppose you want to query parent model and also apply condition on child relation model and you also want to get specifies data, not all data from child table in hasMany relationship.
$collection_products = Product::on('business');
// query()
if($selectedColor != null){
$collection_products = $collection_products->with(['variants' => function($query) use ($selectedColor){
$query->where('color','=',$selectedColor);
}]);
$collection_products = $collection_products->whereHas('variants',function(Builder $query) use ($selectedColor){
$query->where('color','=',$selectedColor);
});
}else{
$collection_products = $collection_products->with('variants');
}
$collection_products = $collection_products->with('product_images');
$collection_products = $collection_products;
->get();
Comments
Post a Comment