Laravel 5.1中的多个条件

时间:2017-06-02 10:20:22

标签: laravel eloquent laravel-5.1 laravel-eloquent

我的多重过滤器实现中存在问题。我有一些复选框传递给AJAX一些可以有多个值的参数。 在我的控制器中,我写了这个来处理这个参数:

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正确重新加载但结果保持不变。只有当我使用不同的场景时才能使它工作:

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的内容,但我需要更多帮助

2 个答案:

答案 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';
    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);
}

答案 1 :(得分: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是数组 默认。如果它是对象,则输入到数组并使用in 查询。
  • 首先删除检查计数的条件 在所有3个阵列中,然后尝试建立一个查询。
  • 创建了一个新变量$rst,其默认值为null。在满足任何条件时,它将查询对象分配给$ rst。
  • 最后检查是$ rst不等于null并调用其paginate方法。

希望这会清除你并解决你的问题。如果没有,至少你会知道如何重新计算你的代码:)

相关问题