Problem of Laravel routes

Asked

Viewed 252 times

-1

I’m having a problem with a route of my system.

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

From what I understand he is interpreting the "list" in "/updates/list" as if it were a value. Does anyone have any idea how to solve?

Error:

invalid input syntax for integer: "list " (SQL: select * from "ger_update " Where "att_code " = list limit 1)"

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 supposed to enter the function "list" but it enters the function "show"

  • Face this is database problem... not the route itself, detail more your problem, post the controller code and if possible the description of the tables involved in the controller.

  • I edited the question with the controller code

  • By chance some of your routes have the same name or the same url?

  • I’m using apiResource on the routes, except for this route I put in the question

  • put the following command somewhere and show us the result: foreach (Route::getRoutes() as $route) {
 var_dump($route->getUri());
}

  • 2

    You probably set before these routes a Route::resource. In Laravel, if using Resource with other routes, always set the routes before declaring the resource.

  • @Wallacemaxters Valeu, that was the problem, but now the "store" function of Resource stopped working

  • @João.Mistura I left answered your question, to help other users. Consider marking the answer as accepted, if you think you should do this.

Show 3 more comments

1 answer

4


The way I see it, it looks like you’re wearing Route::resource before setting the route atualizacoes.listar.

You need to remember that when you use Route::resource, it will create a route that has the second segment¹ of the 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 route "/atualizacoes/list" after declaring the resource, Laravel will search for the declared routes first, when you make the request.

That is to say atualizacoes/{atualizacao} has priority over /atualizacoes/list. This makes it list is recognized as the parameter of your route atualizacoes/{atualizacao}, and not as segment¹ of atualizacoes/list.

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

Remarks:

¹ - The versions I used of Laravel 3 and 4, called each part separated by / of "segment", so I used the word "segment". For example, eu/sou/bonito has 3 segments.

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

  • I understand, but I have another question. In my file of routes I organized them by item, for example, the Update wheels put together, the User routes put together tb. In case I need to put the Resource routes last independent of the Controller I’m using? I don’t know if it’s clear my question.

  • @Last mix I speak is always after the manual statement. Example: Route::get('x/y', 'XController@y'); Route::resource('x', 'XController'); Route::get('quauqer_outra_rota');

  • Got it, thanks partner, you helped me a lot

Browser other questions tagged

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