I use Laravel 5.6, I have 3 Models :
Area has many Stores :
public function stores()
{
return $this->hasMany('Beproxy\App\Models\Store');
}
Store belongs to one Merchant :
public function merchant()
{
return $this->belongsTo('Beproxy\App\Models\Merchant');
}
In my Merchant, I have one tinyInt to define if the Merchant is active or not (state)
Can I write a new function in my Area Model to get all Stores which belong to an active Merchant ?
With hasMany relation, I can use :
public function storesWithProducts()
{
return $this->hasMany('App\Models\Store')->has('products');
}
But I don't find how to use a condition for belongsTo, I tried in my Area, but it loads everything :
public function storesWithMerchantActive()
{
return $this->hasMany('App\Models\Store')
->with(['merchant' => function($query) {
$query->where('state', '=', 1);
}])
->has('products');
}
I think you just need to update this function may help.
public function merchant()
{
return $this->belongsTo('Beproxy\App\Models\Merchant')
->where('state','1');
}
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