I have hasMany relation in User model:
public function wins() {
return $this->hasMany(Win::class, 'user_id', 'id');
}
I want to get records from winners table, “where user_id = 5865535 AND (app_id = 730 OR state = ‘new’)”
$user = AppUser::find(5865535);
$wins = $user->wins()->where('app_id', 730)->orWhere('state', 'new')->pluck('user_id')->unique();
But I got records with different user_id
0 => 4909807
18 => 4909808
143 => 4909867
144 => 4909868
145 => 4909870
148 => 4909871
271 => 4916186
312 => 5865535
I have fix like this:
$wins = $user->wins()->where(function ($query) {
$query->where('app_id', 730)->orWhere('state', 'new');
})->first();
Output:
0 => 5865535
But I want to know why my first variant is not working? Maybe this is a bug?
Laravel version: 9.52.16
PHP version: 8.1.13
1