如何在Laravel中按搜索词条件过滤?

时间:2019-05-28 07:26:47

标签: php mysql database laravel

每个人都编写了一个代码,以按性别,国籍,目的和年龄过滤搜索条件,但是这些应用程序都可以单独工作。我想做一个结果。 我想知道如何立即应对各种情况。例如,如果一次听到性别和年龄,我必须返回其结果,我想知道如何做。 我想在这里解决两件事。 1.对所有搜索结果应用条件叠加 2.进入没有搜索的页面时,没有打印列表

//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 search
     if (!empty($request->goal) && $request->goal !== 'all') {

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


    //age search
    if($request->ageMin != 1 || $request->ageMax != 100 ){

        $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])
        ->orderBy('penpals.created_at','desc');

    }


    //search result
    $penpalsData = $penpals->orderBy('penpals.created_at','desc')->paginate(12); 
$penpalsCount = count($penpalsData);

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

1 个答案:

答案 0 :(得分:0)

我要做的就是在搜索过滤器时构建对象。

您当前正在做的是应用过滤器,并突然结束每个过滤器。

首先应用每个存在的过滤器,然后最后执行搜索。

这样做:

add filter (age).
add filter (nationality).
-> then finally execute search.

不是:

filter (age) -> search. execute.
filter (nationality) -> search. execute.

这是主意(当然这未经测试):

<?php


// $penpals =  $this->penpalModel->getUsers()->latest()->paginate(12);
// base penpals object
$penpals = $this->penpalModel;
// start out with the initial original object, untouched

// if nickname is present, add filter nickname
if (!empty($request->name)) {
    $users = $this->userModel->where('name', 'like', '%' . $request->name . '%')->get();
    if (!empty($users)) {
        // apply | attached user ids found
        $penpals->whereIn('user_id', $users);
    }        
}
// dont cut off the query and execute yet, continually check other filters
if (!empty($request->gender) && $request->gender !== 'all') { 
    $penpals->leftJoin('users', 'penpals.user_id', '=', 'users.id')
    ->select('penpals.*', 'users.gender')
    ->where('users.gender', $request->gender); 
}
// again, continue to the next set of filters (country search)
// and so on, the same syntax, build off of the base object and continually connect filters

// then on the end, EXECUTE it!

$penpalsData = $penpals->orderBy('penpals.created_at','desc')->paginate(12); // execute then builder after several layers of filters are applied!
$penpalsCount = count($penpalsData);


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

只要遵循这个想法或概念,这不是您复制答案和工作类型的方法。对其进行修改,使其适合您的业务逻辑。