2017-06-02 61 views
-1

我在多個過濾器實現中遇到問題。我有一些複選框傳遞給AJAX一些可以有多個值的參數。 在我控制我寫了這個處理這個PARAMS:多條件在Laravel 5.1

function getCategoria(Request $request) { 
    $path_info = Request::getPathInfo(); 
    $path = substr($path_info, 1); 
    $links = explode('/', $path); 
    $categorie = \App\Models\Categorie::where('primaria',1)->get(); 
    $categoria = \App\Models\Categorie::where('link_statico', $path)->first(); 
    $categoriaz = \App\Models\Categorie::where('link_statico', $path)->first(); 
    $id = ucfirst($links[0]); 

    $prodottip = \App\Models\Prdotticategorie::where('prodotti2categorie.id_categoria', $categoriaz->id)->join('prodotti', 'prodotti.id', '=', 'prodotti2categorie.id_prodotto')->query(); 

    $brands = Input::get('brands'); 
    $genere = Input::get('genere'); 
    $stagione = Input::get('stagione'); 
    $this->data['links'] = $links; 
    $this->data['categorie'] = $categorie; 
    $this->data['categoria'] = $categoria; 
    $this->data['categoriaz'] = $categoriaz; 
    $this->data['id'] = $id; 
    $this->data['pages'] = 'categorie.frontend'; 
    if(count($brands) > 0 && count($genere) > 0 && count($stagione) > 0) 
    { 
     if(count($brands) > 0) 
     { 
      $brands_array = []; 
       if(is_array($brands) || is_object($brands)) 
       { 
        foreach ($brands as $brand) 
        { 
         $brands_array[] = $brand; 
        } 
         $rst = $prodottip->whereIn('prodotti.id_produttore', $brands_array); 
       } 
     } 
     if(count($genere) > 0) 
     { 
      $genere_array = []; 
       if(is_array($genere) || is_object($genere)) 
       { 
        foreach ($genere as $gen) 
        { 
         $genere_array[] = $gen; 
        } 
         $rst = $prodottip->whereIn('prodotti.genere', $genere_array); 
       } 
     } 
     if (count($stagione) > 0) 
     { 
      $stagione_array = []; 
       if(is_array($stagione) || is_object($stagione)) 
       { 
        foreach ($stagione as $stag) 
        { 
         $stagione_array[] = $gen; 
        } 
         $rst = $prodottip->whereIn('prodotti.stagione', $stagione_array); 
       } 
     } 
     $prodottix = $rst->paginate(18); 
    } else { 
     $prodottix = $prodottip->paginate(18); 

    } 
    $this->data['prodottix'] = $prodottix; 
    if (Request::ajax()) { 
     $page = 'layouts.'.CNF_THEME.'.categorie_ajax'; 
     $view = view($page, $this->data)->render(); 
     return response()->json(['html'=>$view]); 

    } 
    $page = 'layouts.'.CNF_THEME.'.categorie'; 
    return view($page, $this->data); 
} 

的問題是,AJAX正確加載,但結果保持不變。我可以把它唯一的工作,如果我用不同的場景的ELSEIF這樣的:

if(count($brands) > 0 && count($genere) > 0 && count($stagione) > 0) 
    //query with 3 where 
elseif(count($brands) > 0 && count($genere) == 0 && count($stagione) == 0) 
    // query with 1 where 

嗨已經閱讀在Laravel DynamicScopes的東西,但我需要更多的幫助THKS

+0

你可以在用戶或者其他地方爲多個條件。例如where() - > orWhere() –

+0

您的代碼嚴重混淆。在這種情況下,如果(count($ brands)> 0 && count($ genere)> 0 && count($ stagione)> 0)則檢查是否所有數組都有一些值,並且再次檢查相同並應用條件。我建議您只需查看一次代碼並刪除不需要的內容,以便我們可以幫助您回答。 –

回答

0
function getCategoria(Request $request) 
{ 
    $path_info = Request::getPathInfo(); 
    $path = substr($path_info, 1); 
    $links = explode('/', $path); 
    $categorie = \App\Models\Categorie::where('primaria', 1)->get(); 
    $categoria = \App\Models\Categorie::where('link_statico', $path)->first(); 
    $categoriaz = \App\Models\Categorie::where('link_statico', $path)->first(); 
    $id = ucfirst($links[0]); 

    $prodottip = \App\Models\Prdotticategorie::where('prodotti2categorie.id_categoria', $categoriaz->id)->join('prodotti', 'prodotti.id', '=', 'prodotti2categorie.id_prodotto')->query(); 

    $brands = Input::get('brands'); 
    $genere = Input::get('genere'); 
    $stagione = Input::get('stagione'); 
    $this->data['links'] = $links; 
    $this->data['categorie'] = $categorie; 
    $this->data['categoria'] = $categoria; 
    $this->data['categoriaz'] = $categoriaz; 
    $this->data['id'] = $id; 
    $this->data['pages'] = 'categorie.frontend'; 
    $rst = null; 
    if (count($brands) > 0) { 
     $brands = is_object($brands) ? array($brands) : $brands; 
     $rst = $prodottip->whereIn('prodotti.id_produttore', $brands); 
    } 

    if (count($brands) > 0) { 
     $genere = is_object($genere) ? array($genere) : $genere; 
     $rst = $prodottip->whereIn('prodotti.genere', $genere); 
    } 

    if (count($stagione) > 0) { 
     $stagione = is_object($stagione) ? array($stagione) : $stagione; 
     $rst = $prodottip->whereIn('prodotti.stagione', $stagione); 
    } 

    if(null !== $rst) { 
     $prodottix = $rst->paginate(18); 
    } else { 
     $prodottix = $prodottip->paginate(18); 
    } 

    $this->data['prodottix'] = $prodottix; 
    if (Request::ajax()) { 
     $page = 'layouts.' . CNF_THEME . '.categorie_ajax'; 
     $view = view($page, $this->data)->render(); 

     return response()->json(['html' => $view]); 
    } 

    $page = 'layouts.' . CNF_THEME . '.categorie'; 
    return view($page, $this->data); 
} 
  • 考慮到$brands,$genere,$stagione是由 排列的默認值。如果它是對象然後鍵入轉換爲數組並使用 查詢。
  • 刪除第一條件是檢查所有3個數組的計數 ,然後嘗試構建查詢。
  • 創建新變量$rst,其默認值爲null。在任何條件都滿足的情況下,它會將查詢對象分配給$ rst。
  • 最後的檢查首先不等於null並且調用它的paginate方法。

希望這將清除你並解決你的問題。如果沒有,至少你會得到一個想法,你如何能夠重新分解你的代碼:)

+0

工作也是如此! !很多thks! – rubenSousa

0
function getCategoria(Request $request) { 
    $path_info = Request::getPathInfo(); 
    $path = substr($path_info, 1); 
    $links = explode('/', $path); 
    $categorie = \App\Models\Categorie::where('primaria',1)->get(); 
    $categoria = \App\Models\Categorie::where('link_statico', $path)->first(); 
    $categoriaz = \App\Models\Categorie::where('link_statico', $path)->first(); 
    $id = ucfirst($links[0]); 

    $prodottip = \App\Models\Prdotticategorie::where('prodotti2categorie.id_categoria', $categoriaz->id)->join('prodotti', 'prodotti.id', '=', 'prodotti2categorie.id_prodotto')->query(); 

    $brands = Input::get('brands'); 
    $genere = Input::get('genere'); 
    $stagione = Input::get('stagione'); 
    $this->data['links'] = $links; 
    $this->data['categorie'] = $categorie; 
    $this->data['categoria'] = $categoria; 
    $this->data['categoriaz'] = $categoriaz; 
    $this->data['id'] = $id; 
    $this->data['pages'] = 'categorie.frontend'; 
    if(count($brands) > 0 && count($genere) > 0 && count($stagione) > 0) 
    { 
     if(count($brands) > 0) 
     { 
      $brands_array = []; 
       if(is_array($brands) || is_object($brands)) 
       { 
        foreach ($brands as $brand) 
        { 
         $brands_array[] = $brand; 
        } 
         $prodottip = $prodottip->whereIn('prodotti.id_produttore', $brands_array); 
       } 
     } 
     if(count($genere) > 0) 
     { 
      $genere_array = []; 
       if(is_array($genere) || is_object($genere)) 
       { 
        foreach ($genere as $gen) 
        { 
         $genere_array[] = $gen; 
        } 
         $prodottip = $prodottip->whereIn('prodotti.genere', $genere_array); 
       } 
     } 
     if (count($stagione) > 0) 
     { 
      $stagione_array = []; 
       if(is_array($stagione) || is_object($stagione)) 
       { 
        foreach ($stagione as $stag) 
        { 
         $stagione_array[] = $gen; 
        } 
         $prodottip = $prodottip->whereIn('prodotti.stagione', $stagione_array); 
       } 
     } 
    } 

    $prodottix = $prodottip->paginate(18); 

    $this->data['prodottix'] = $prodottix; 
    if (Request::ajax()) { 
     $page = 'layouts.'.CNF_THEME.'.categorie_ajax'; 
     $view = view($page, $this->data)->render(); 
     return response()->json(['html'=>$view]); 

    } 
    $page = 'layouts.'.CNF_THEME.'.categorie'; 
    return view($page, $this->data); 
} 
+0

非常感謝不幸的是有人打電話給未定義的方法照亮\數據庫\查詢\建設者::查詢() – rubenSousa

+0

從這一行刪除最後'query()'$ prodottip = \ App \ Models \ Prdotticategorie :: where(' prodotti2categorie.id_categoria',$ categoriaz-> id) - > join('prodotti','prodotti.id','=','prodotti2categorie.id_prodotto') - > query();',然後嘗試 –