What you are looking for is this whereHas(). You can also write with(array('products.skus'))directly withoutproducts
$category = new Category();
$categories = $category->with(array('products', 'products.skus' => function ($query) {
$query->where('price', '>', 10);
}))
->whereHas('products.skus', function($query){
$query->where('price', '>', 10);
})->get();
You need both, withand whereHas, but you can simplify the code a bit by placing the closure in a variable:
$priceGreaterTen = function($query){
$query->where('price', '>', 10);
};
$category = new Category();
$categories = $category->with(array('products', 'products.skus' => $priceGreaterTen))
->whereHas('products.skus', $priceGreaterTen)
->get();
source
share