How to get the GET value in Laravel, even when the request method is POST?

Asked

Viewed 7,856 times

2

I’m making a request for a route, where the request method is marked with the Any - that is, accepts any method.

public function anyIndex()
{
   $nome = Input::get('nome');

   $id   = Input::get('id'); // Queria específico do `GET`, não qualquer um
}

URL:

 'usuarios/index?nome=Wallace+Souza&id=Teste'

The problem is that, no Laravel, the method Input::get serves for everything (POST, PUT, GET). And, in this specific case, I want to take only the value of the query string.

That is, if I fill in the field id on the form, and send a request POST, still yes I want to take only what is past in the url via GET.

You can do it in Laravel?

2 answers

4


Come on, I’ll answer my own question, but don’t get it wrong, I just want to help some people who are interested to learn more about the Laravel.

When you want to take the values referring only to what is in the query string, ignoring what is in the POST and others, you can use the method Request::query.

Example - Laravel 5:

  public function anyIndex(Request $request)
  {
      $id = $request->query('id'); // Pega somente o da query string
  }

Example - Laravel 4:

public function getIndex()
{
      $id = Request::query('id');
}

If you still want to take all the values of the method GET passed as a parameter of url, you can call this method without argument.

  Request::query();
  • in that case I don’t need to create the options there on the route? type the /{parameter} I quoted? I can leave only the /index that the data will appear in the url without being set in the route and still I will be able to pick up the controller?

  • 1

    @Raylansoares in this case I’m talking about the parameters GET. Maybe you’re getting confused pages/15 with pages/?id=15. When I speak GET I refer to the last example

  • Ahh yes, true. Thank you! rs

2

I guess you just take the variable you used to receive on the route, no?

Like, if you sent only one parameter via url, in your route you must have a {parameter}, in your controller you get it with $parameter. Ex:

Calling for:

<form action="{{ url('/index',  '05') }}>

Route:

Route::any('/index/{idPassada}', 'MeuController@anyIndex');

Controller:

public function anyIndex(Request $request, $idPassada)
{
   $nome = Input::get('nome');
   $id   = $idPassada;
}
  • That’s not the GET. This is about tracking the route. Anyway, thanks for the answer. It’s a good option... +1 ....

Browser other questions tagged

You are not signed in. Login or sign up in order to post.