2017-06-02 62 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();',然后尝试 –