A
A
Analka2020-01-17 12:32:09
Laravel
Analka, 2020-01-17 12:32:09

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();

but nothing works
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

3 answer(s)
K
Konstantin B., 2020-01-17
@Kostik_1993

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.

A
Anton, 2020-01-17
@karminski

$gender = 0; 
if (Auth::user()->attributes->gender == 1){
    $gender = 2; 
}
else if (Auth::user()->attributes->gender == 2) { // Тут у вас ошибка
    $gender = 1;
}

J
jazzus, 2020-01-17
@jazzus

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;
}

and receive via
$user->FullName

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question