Oto Shavadze
Oto Shavadze

Reputation: 42773

Multi optional parameters for controller

I need pass 2 optional parameters to controller, in route I have:

Route::get('/products/{start?}/{end?}', 'productsController@index');

In Products controller I have:

public function index($start = NULL, $end = NULL) {
    // ....
}

This works fine if there is passed both of arguments, but how to handle situation, were we need pass only $end parameter? because when passed only one parameter, it always goes to as $start parameter.

Upvotes: 1

Views: 91

Answers (2)

ceresAlibaba
ceresAlibaba

Reputation: 56

I think you should cover route add param products?start=&end=

Route::get('/products', 'productsController@index');

public function index() {
    $start = request()->input('start');
    $end = request()->input('end');
     // ....
}

you easy handle it.

Upvotes: 4

VIKAS KATARIYA
VIKAS KATARIYA

Reputation: 6005

In your Route.php file

Route::get('/products', 'productsController@create');

In your controller file

public function create(Request $request) {
    $start = $request->start;
    $end = $request->end;
}

or

Route::get('/products/{start?}/{end?}', 'productsController@index');

public function index($start= null, $end= null)
    {
        $variable = DB::table('name table')->paginate(16);
        if($start!=null) {
            //some code
        }
        if($end!=null) {
            //some code
        }
        return somethingt;
    }

Upvotes: 2

Related Questions