Closed mateo2181 closed 5 years ago
This is going to be because you're chaining with OR
outside of subqueries. A model filter runs methods in the order it encounters the input. With your current model filter you're creating:
$sport = 8;
$residence = 12;
User::whereHas('athlete', function($q) use ($sport) {
return $q->where('sport_id', $sport)->orWhere('secondary_sport_id', $sport);
})
->orWhereHas('scout', function($q) use ($sport) {
return $q->where('sport_id', $sport);
})
->whereHas('athlete', function($q) use ($value) {
return $q->where('residence_country_id', $residence);
})
->orWhereHas('scout', function($q) use ($value) {
return $q->where('residence_country_id', $residence);
});
Which is why only one of your constraints is being matched.
Wrapping any method using an OR
statement in a subquery would fix this:
<?
class UserFilter extends ModelFilter
{
public $relations = [];
public function fullname($name)
{
return $this->where(function($q) use ($name)
{
$names = explode(" ",$name);
return $q->where('firstname', 'LIKE', "%$name%")
->orWhere('lastname', 'LIKE', "%$name%")
->orWhereIn('lastname',$names)
->whereIn('firstname',$names)
->orWhere('email','LIKE',"%$name%");
});
}
public function sport($sport)
{
return $this->where(function($query) use ($sport)
{
$this->whereHas('athlete', function($q) use ($sport) {
return $q->where('sport_id', $sport)->orWhere('secondary_sport_id', $sport);
})->orWhereHas('scout', function($q) use ($sport) {
return $q->where('sport_id', $sport);
});
});
}
public function gender($value)
{
return $this->whereHas('athlete', function($q) use ($value) {
return $q->where('gender', $value);
});
}
public function age($value)
{
$arrayNumbers = explode('-',$value);
$ageFrom = (int)$arrayNumbers[0];
$ageTo = (int)$arrayNumbers[1];
$from = Carbon::now()->subYears((int)($ageTo+1));
$from = $from->format('Y-m-d');
$to = Carbon::now()->subYears($ageFrom)->format('Y-m-d');
\Log::info($from);
\Log::info($to);
if($this->input('typeUser') == 'Scout' || $this->input('typeUser') == '') return true;
return $this->related('athlete', function($q) use ($from,$to) {
return $q->where('date_of_birth','>', $from)->where('date_of_birth','<=', $to);
});
}
public function status($value)
{
return $this->whereHas('athlete', function($q) use ($value) {
return $q->where('status', $value);
});
}
public function sportingGoal($value)
{
return $this->whereHas('athlete', function($q) use ($value) {
return $q->where('sporting_goal', $value);
});
}
public function residenceCountry($value)
{
return $this->where(function($query) use ($value)
{
return $query->whereHas('athlete', function($q) use ($value) {
return $q->where('residence_country_id', $value);
})->orWhereHas('scout', function($q) use ($value) {
return $q->where('residence_country_id', $value);
});
});
}
public function nationality($value)
{
return $this->where(function($query) use ($value)
{
return $this->whereHas('athlete', function($q) use ($value) {
return $q->where('nationality_id', $value);
})->orWhereHas('scout', function($q) use ($value) {
return $q->where('nationality_id', $value);
});
});
}
}
Thanks you!! Now works perfectly!
I want to filter related models with one to many relations but always is taken only the first parameter. For example: http://localhost:8000/p/profile/search?sport=8&residence_country=12 return only users with sport id 8 but not with residence country id 12. http://localhost:8000/p/profile/search?residence_country=12&sport=8 return only users with residence country id 12 but not with sport id 8 .
Someone had this problem?
USER MODEL:
ATHLETE MODEL:
SCOUT MODEL:
USER FILTER: