Support the ongoing development of Laravel.io →
Requests Laravel Input

Hello !

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 ?

Last updated 2 years ago.
0

Sign in to participate in this thread!

Eventy

Your banner here too?

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2025 Laravel.io - All rights reserved.