vendredi 2 juin 2017

Multiple where conditions in Laravel 5.1

i have a problem in my multiple filters implementation. I have some checkboxes that pass to AJAX some params that can have multiple values. In my controller i have written this to handle this params:

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

The problem is that AJAX reload correctly but the results remaining the same. I can bring it working only if i an elseif with different scenarios like this:

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

Hi have read something on DynamicScopes in Laravel, but i need more help thks



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire