Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
262 views
in Technique[技术] by (71.8m points)

how to access query string in controller and routes of laravel

when I will click on a tag it go to specific url if no id found then go to reference My view

<a href="{{URL::to('user/')}}/?reference={{$user->username}}">
                    go there
                  </a>

my route

Route::get('/user/{id}/{reference?}', 'UserController@index')->name('/user');
question from:https://stackoverflow.com/questions/65679917/how-to-access-query-string-in-controller-and-routes-of-laravel

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

The query string is considered part of the inputs and not part of the URI for routing. If you want the 'id' part of the URI to be optional you would have to adjust your definition. Then in your controller you can access your reference query string parameter.

Route::get('user/{id?}', 'UserController@index')->name('user');

public function index(Request $request, $id = null)
{
    $reference = $request->input('reference');
    ...
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...