簡體   English   中英

如何在Laravel中按搜索詞條件過濾?

[英]How can I filter by search term condition in 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
        ]);

我要做的是在搜索過濾器時構建對象。

您當前正在執行的操作是應用過濾器,並突然將其終止。

應用首先出現的每個過濾器,然后最后執行搜索。

這樣做:

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

只要遵循這個想法或概念,這不是您復制答案和工作類型的方法。 對其進行修改,使其適合您的業務邏輯。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM