Answer the question
In order to leave comments, you need to log in
Laravel filter by multiple parameters?
tell me how to implement a user filter by 10 parameters?
I try like this
$gender = 0;
if (Auth::user()->attributes->gender == 1){
$gender = 2; //Выводим деушек , если текущий пользователь парень
}
if (Auth::user()->attributes->gender == 2){
$gender = 1; //Выводим парней , если текущий пользователь девушка
}
$users = User::filter($request->all(),$gender)
->orderBy('users.id','asc')->get();
public function scopeFilter($query,$request,$gender)
{
$query->leftJoin('user_attributes','user_attributes.user_id',
'=','users.id');
$query->select('users.id','users.slug','users.name',
'user_attributes.fullname','user_attributes.age',
'user_attributes.avatar',
'user_attributes.city');
$query->where('user_attributes.gender','=', $gender);
return $query;
}
Answer the question
In order to leave comments, you need to log in
Well, for starters, I would advise you to look at what request you get as a result. If an error is returned, you can reset it here as well.
$gender = 0;
if (Auth::user()->attributes->gender == 1){
$gender = 2;
}
else if (Auth::user()->attributes->gender == 2) { // Тут у вас ошибка
$gender = 1;
}
Is the user's gender filled? dd($gender) returns what? Why request in an osprey? Or is it a truncated code? For debug here you need to post the exact one. Why not through relationships done? I would stuff all the specified fields from the attributes into users and it is possible without an attribute table at all.
Well, or whereHas if through relationships, but you definitely need to store gender not in attributes. FullName can be collected in the model
public function getFullNameAttribute()
{
return $this->name.' '.$this->lastname;
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question