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

2

I am making a request for a route, where the request method is marked with Any - that is, it 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 in Laravel , the Input::get method is for everything ( POST , PUT , GET ). And in this specific case, I want to get only the value of the query string.

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

Can you do this in Laravel ?

    
asked by anonymous 22.04.2016 / 21:32

2 answers

3

Come on, I'll answer my own question, but do not misunderstand it, I just want to help some people who are interested in learning more about Laravel .

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

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 get all values of the GET method passed as a parameter of url , you can call this method without argument.

  Request::query();
    
22.04.2016 / 22:25
2

I think it's just you get the variable that you used to get on the route, right?

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

Call:

<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;
}
    
22.04.2016 / 22:16