Search code examples
laravellaravel-5laravel-routing

How to pass optional ID to Laravel Controller from Route?


I want to pass an ID to my controller from Routes:

Route::get('user/{id?}', function ($id= null) {
    return $id;
});

However, I'm not sure where to put the controller name and function?

MyController@get

Laravel's documentation and other questions don't seem to be related to this issue.

What am I missing here?


Solution

  • This is simple for you. You almost done

    Route::get('user/{id?}', YourControllerNameController@get);
    

    In your controller

    public function get($id = null){
    
    }