Reputation: 135
I have a query working perfect but I need it in ORM Laravel. I just need this query in eloquent Query builder.
SELECT * FROM product where vendor_id in (
SELECT vendor_id from vendor where is_block = 0
);
Scenario : There are two tables. One for
Product
and other forVendor
. I want to select product fromProduct
table. Each product belong to a vendor. If vendor is blocked then don't select product. Ifis_block
attribute is0
then its mean thatvendor
is not block. Ifis_blocked=1
then vendor is blocked. I need to select only products of a vendor whose is_block = 0.
Upvotes: 1
Views: 65
Reputation: 163748
Use the whereHas()
method:
Product::whereHas('vendor', function($q) {
$q->where('is_block', 0);
})->get();
Or the whereDoesntHave()
method:
Product::whereDoesntHave('vendor', function($q) {
$q->where('is_block', 1);
})->get();
Make sure you've defined the relationship in the Product
model:
public function vendor()
{
return $this->belongsTo(Vendor::class);
}
Upvotes: 3