Php 如何为Laravel搜索过滤器功能创建查询语句?

Php 如何为Laravel搜索过滤器功能创建查询语句?,php,mysql,database,laravel,Php,Mysql,Database,Laravel,如何为Laravel搜索过滤器功能创建查询语句? 我想将Laravel搜索查询合并为一个查询。 现在这个代码有一个问题。它不是多条件叠加。我想把这个查询统一成一个查询,得到满足所有条件的结果。我该怎么办 //펜팔 메인 페이지 public function index (Request $request){ //base Object $penpals = $this->penpalModel->getUsers(); //name search

如何为Laravel搜索过滤器功能创建查询语句? 我想将Laravel搜索查询合并为一个查询。 现在这个代码有一个问题。它不是多条件叠加。我想把这个查询统一成一个查询,得到满足所有条件的结果。我该怎么办

//펜팔 메인 페이지
public function index (Request $request){
    //base Object
    $penpals =  $this->penpalModel->getUsers();


    //name search
    if (!empty($request->name)) {
        $users = $this->userModel->where('name', 'like', '%' . $request->name . '%')->get();
        if (!empty($users)) {

            $penpals->whereIn('user_id', $users);
        }        
    }


    //gender search
    if (!empty($request->gender) && $request->gender !== 'all') { 
        $penpals->leftJoin('users', 'penpals.user_id', '=', 'users.id')
        ->select('penpals.*', 'users.gender')
        ->where('users.gender', $request->gender); 
    }


    //country search
    if (!empty($request->country) && $request->country !== 'all') { 
        $penpals->leftJoin('users', 'penpals.user_id', '=', 'users.id')
        ->select('penpals.*', 'users.country')
        ->where(['users.country', $request->country]); 
    }


     // goal name search
     if (!empty($request->goal) && $request->goal !== 'all') {

        $penpals = $this->penpalModel->where('goal_id',$request->goal)->latest();
     }


    //age search
    if($request->ageMin || $request->ageMax){

        $ageMin = floor($request->ageMin);
        $ageMax = floor($request->ageMax);

        $penpals = $this->penpalModel->leftJoin('users', 'penpals.user_id', '=', 'users.id')
        ->select('penpals.*', 'users.age')
        ->whereBetween('users.age', [$ageMin, $ageMax]);
    }




    $penpalsData = $penpals->orderBy('penpals.created_at','desc')->paginate(12);




    $penpalsCount = count($penpalsData);

    return view('penpal.index')->with([
        'penpals'       => $penpalsData,
        'penpalsCount'  => $penpalsCount
        ]);

}

您应该将关系
用户
添加到penpal模型

class Penpal extends Model {
    ...

    public function user()
    {
        return $this->belongsTo(User::class);
    }

    ...
}
然后您可以使用
whereHas
雄辩的方法:

public function index (Request $request){
    //query builder
    $query =  $this->penpalModel->query();

    //user fields
    if(
        $request->name || 
        $request->ageMin ||
        $request->ageMax ||
        ($request->gender && $request->gender !== 'all') ||
        ($request->country && $request->country !== 'all')
    ){
        $query->whereHas('user', function($query) use ($request){
            if($request->name){
                $query->where('name', 'like', '%' . $request->name . '%');
            }

            if($request->gender && $request->gender !== 'all'){
                $query->where('gender', $request->gender);
            }

            if($request->country && $request->country !== 'all'){
                $query->where('country', $request->country);
            }

            if($request->ageMin){
                $query->where('age', '>', floor($request->ageMin));
            }

            if($request->ageMax){
                $query->where('age', '<', floor($request->ageMax));
            }
        })
    }

    //goal
    if ($request->goal && $request->goal !== 'all') {
        $query->where('goal_id', $request->goal);
    }

    $penpals = $query
        ->with('user')
        ->latest()
        ->paginate(12);

    return view('penpal.index')->with([
        'penpals'       => $penpals,
        'penpalsCount'  => $penpals->count() //use $query->count() to get count of all results in db, not only in paginator collection
    ]);
}

您应该将关系
用户
添加到penpal模型

class Penpal extends Model {
    ...

    public function user()
    {
        return $this->belongsTo(User::class);
    }

    ...
}
然后您可以使用
whereHas
雄辩的方法:

public function index (Request $request){
    //query builder
    $query =  $this->penpalModel->query();

    //user fields
    if(
        $request->name || 
        $request->ageMin ||
        $request->ageMax ||
        ($request->gender && $request->gender !== 'all') ||
        ($request->country && $request->country !== 'all')
    ){
        $query->whereHas('user', function($query) use ($request){
            if($request->name){
                $query->where('name', 'like', '%' . $request->name . '%');
            }

            if($request->gender && $request->gender !== 'all'){
                $query->where('gender', $request->gender);
            }

            if($request->country && $request->country !== 'all'){
                $query->where('country', $request->country);
            }

            if($request->ageMin){
                $query->where('age', '>', floor($request->ageMin));
            }

            if($request->ageMax){
                $query->where('age', '<', floor($request->ageMax));
            }
        })
    }

    //goal
    if ($request->goal && $request->goal !== 'all') {
        $query->where('goal_id', $request->goal);
    }

    $penpals = $query
        ->with('user')
        ->latest()
        ->paginate(12);

    return view('penpal.index')->with([
        'penpals'       => $penpals,
        'penpalsCount'  => $penpals->count() //use $query->count() to get count of all results in db, not only in paginator collection
    ]);
}
query()的角色是什么?将querybuilder对象注入对象?query()的角色是什么?将querybuilder对象注入到对象中?