How can i query relationship and still include models that has no relationships? There are two models
Store
Product
code
// Store
public function products() {
$this->belongsToMany('App\Store');
}
// Product
public function stores() {
$this->belongsToMany('App\Product');
}
and a pivot table to connect them called product_store
. Some stores don't have any products. How do i query all products, even those who doesn't belong to any store like:
Product::where('store.id', '=', 1)->get()
this is how i currently do it.
Product::whereHas('stores', function($query) {
$query->where('id', '=', $store_id);
});
but as the laravel docs mention this
Retrieves all products with at least one store
Product::doesntHave('stores')->orWhereHas('stores', function($query) {
$query->where('id', '=', $store_id);
})->get();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With