I have three models
Invoice
Studio
R_studio_pay
Relationships are as follows
studio hasMany Invoice
studio hasOne R_studio_pay
I need to get invoices->where('recurring', 'single')->get() if a column is_r is 1 in R_studio_pay else i don't want the where clause.
I tried using whereHas studio-> wherehas R_studio_pay but conditional where cannot be done.
$invoices = invoice::with('studio')->whereHas('studio', function($query) {
$query->whereHas('r_studio_pay', function($query) {
$query->where('is_r', 1);
});
})->where('recurring', 'single')
But couldn't apply conditional where.
You are on the right track. However, you should add a return statement in your callbacks. return $query->where('is_r', 1);
instead of $query->where('is_r', 1);
. Try the code below.
$invoices = invoice::with('studio')->whereHas('studio', function($query){
return $query->whereHas('r_studio_pay', function($query){
return $query->where('is_r', 1);
});
})->where('recurring', 'single');