Route problem Laravel

-1

I'm having a problem with a route from my system.

Route::get('/atualizacoes/list', 'AtualizacoesController@listarIndex')->name('atualizacoes.listar');

By what I understand it is interpreting the "list" in "/ updates / list" as if it were a value. Anyone have any ideas how to resolve?

Error:

  

Invalid input syntax for integer: \ "list \" (SQL: select * from \ "ger_address \" where \ "att_codigo \" =

Controller code:

public function show($id){
    $atualizacao = GerAtualizacao::where('att_codigo', $id)->first();

    return response()->json($atualizacao);
}

public function listarIndex(){
    $atualizacoes = GerAtualizacao::all();

    return response()->json($atualizacoes);
}

It was for it to enter the "listarIndex" function but it enters the "show" function

    
asked by anonymous 22.10.2018 / 15:51

1 answer

4

As I see it, it looks like you're using Route::resource before setting the atualizacoes.listar route.

You need to remember that when you use Route::resource , it will create a route that has the second segment¹ of path as a variable value.

Example:

 Route::resource('usuarios', 'UsuariosController');

Will generate:

GET  /usuarios
GET  /usuarios/{usuario} # preste atenção nessa
POST /usuarios
PUT  /usuarios/{usuario}
DELETE /usuarios/{usuario}

What happens is that when you set the "/atualizacoes/list" route after declaring resource , Laravel will search the declared routes first when you make the request.

That is, atualizacoes/{atualizacao} has priority over /atualizacoes/list . This causes list to be recognized as the parameter of your atualizacoes/{atualizacao} route, not as% segment of atualizacoes/list .

I recommend in Laravel that whenever you use resource and implement other routes manually, declare the Route::resource of the controller used last.

Notes:

¹ - The versions I used of Laravel 3 and 4 called each part separated by% of segment, so I used the word "segment". For example, / has 3 follow-ups.

I pointed this out in the comments to the AP and he confirmed that this was the problem, I replied to be documented.

    
22.10.2018 / 16:44