I am using the new whereHas
method to add a constraint on an eager loaded relationship in Laravel 4.1.
$broadcast = $broadcast->whereHas('season', function ($query) use ($parameterValues) {
$query->where('name', '2011-12');
});
In this example, I'm searching for a football/soccer broadcast where the relationship is that a Broadcast
belongsTo
a Season
I want to add a constraint for team (club) where the relationship is that a Broadcast
hasOne
homeClub
and a Broadcast
hasOne
awayClub
. I want results for where a team (liverpool) is either the home or the away club so I try to use the orWhereHas
- a featured added in L4.1
$broadcast = $broadcast
->with('homeClub')
->whereHas('homeClub', function($query) use ($parameterValues) {
$query->where('abb', $parameterValues['club_abbs']);
});
$broadcast = $broadcast
->with('awayClub')
->orWhereHas('awayClub', function($query) use ($parameterValues) {
$query->where('abb', $parameterValues['club_abbs'] );
});
But this just seems to cancel out the constraint on the season that's mentioned in my first example. It's like, by chaining the the orWhereHas
, my where
methods have 'forgotten' what they were constraining.
I was using the method incorrectly. I loaded both associations first and then chained the whereHas
and orWhereHas
$broadcast->with('homeClub');
$broadcast->with('awayClub');
$broadcast->whereHas('homeClub', function($query) use ($parameterValues) {
$query->where('abb', 'liverpool');
})->orWhereHas('awayClub', function($query) use ($parameterValues) {
$query->where('abb', 'liverpool');
});
Here, we will learn how to use orwherehas in laravel. we can use eloquent wherehas than orwherehas when we used relationship in laravel application. some time we require to use query orwherehas in laravel 6.
In this example i will show you how to use whereHas and orWhereHas in laravel 6, laravel 7 and laravel 8 application.
Many time we add more than two relation with database model and you have to add where condition with both of that relation model than how you will add where condition with both model.
If you don't understand clearly than i will explain you by example. i have User Model with some records. When i create new user at that time i have two more fields for city and state. i basically i have City and State master. i will add belong to relationship with my User model.
Than my requirement was if i had one search box for city and state. when i search on text box than records should compare with city and state both model. So basically or where condition with both model relationship. At that time i require to use whereHas with orWhereHas in my application. If you have same issue than you can see how i used simple whereHas with orWhereHas in my controller method. So let's see bellow code: Example:
public function myUsers() {
$users = User::with(['city', 'state']);
if ($request->has('name')) {
$users = $users->whereHas('city', function( $query ) use ( $request ){
$query->where('name', $request->name);
})->orWhereHas('state', function( $query ) use ( $request ){
$query->where('name', $request->name);
});
}
$users = $users->get();
dd($users);
}
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