jeudi 15 mars 2018

Handling route with multiple optional parameters in Laravel

I am trying to implement a filtering method for some products.

This is the route:

Route::get('/TVs/{type?}/{producer?}', 'Product\AllProducts@getTVs')->where(['type' => '4kTV|curved|lcd|oled|plasma'], ['producer'=>'Samsung'])->name('TVs');

And this is the controller function:

public function getTVs($type = null, $producer = null)
    {
        $products = DB::table('products')->paginate(16);
        if($type!=null) {
            $products = Product::where('type', $type)->paginate(16);
        }
        if($producer!=null) {
            $products = Product::where('description','like', '%'.$producer.'%')->paginate(16);
        }
        return view('product.TVs', ['products' => $products]);
    }

If I select the type, the page refreshes and shows the results. Then if i enter the producer, again it works. How can i make the route in such a way, that the order of the optional parameters does not matter and i can filter the results no matter the order ?



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire