Jounior Developer
Jounior Developer

Reputation: 155

Use optional parameter in route

I want to use same route in 2 scenarios,

  1. use route without passing parameter
  2. use route with parameter

So, is it possible & how to do it?

Upvotes: 1

Views: 117

Answers (2)

Ankita Dobariya
Ankita Dobariya

Reputation: 831

In Web Php

Route::get('find-services/{service_name?}', 'commonController@find_services')->name('find-services');

and handle the parameters in commonController.php

public function find_services($service_name = null) {// Do something here}

Or Simple use the get method and check parameter in Controller

public function find_services() {
    $input = Request::all();
    if (isset($input['service_name']) AND ! empty($_GET['service_name'])) {
        // Code
    }
}

I hope this one helps and For More Information Here => https://laravel.com/docs/7.x/routing#parameters-optional-parameters

Upvotes: 1

Marwen Jaffel
Marwen Jaffel

Reputation: 807

You can optional parameters like this

Route::get('my-method/{param?}', function ($param= null) {
// your code here
});

Upvotes: 3

Related Questions