简体   繁体   中英

Laravel get unamed “GET” parameter id from URL

I am trying to get a value from the url that looks more or less like this:

http://localhost:8000/new-user/7

This number 7 passed in the url as a parameters is an ID in which i submit from the blade form as a request for an action i perform in the controller but i cant get this value in anyway. This is what i tried so far:

I tried to use this in the controller in which i submitted the form

$request->route('company_id');

I also tried to get this as a proper GET parameter:

<input type="hidden" name="company_id" id="company_id" value="{{app('request')->input('company_id')}}">

and i also tried this:

<input type="hidden" name="company_id" id="company_id" value="{{Input::get('company_id')}}">

and this:

<input type="hidden" name="company_id" id="company_id" value="{{$_GET['company_id']}}">

None os these options work and i still receive an empty value.

Any ideas or suggestions on how can i get this variable?

Thank you!

A route parameter and a query parameter are two different things.

If you have a route defined like this:

/** routes/web.php */

Route::get('/new-user/{id}', 'UsersController@show');

In this case $id is a route parameter. So to get in your blade view you could do:

/** resources/my_view.blade.php */

{{ request()->id }}

So, with a request like the one you used http://localhost:8000/new-user/7 that should output: 7 .

Another case is when you have a query param . These variables doens't need to be defined in the route. For example a call of this type, using the same route defined in the previous example:

GET http://localhost:8000/new-user/7?foo=bar
                                    ^^^^^^^^

In this case the foo=bar can be accessed like this:

{{ request()->query('foo') }} // 'bar'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM