Problem with routes in Laravel

2

This is the content of the laravel routes file (web.php)

Route::prefix('compra')->group(function () {
    Route::get('usuario', 'FrontControllerCompras@pideUsuario');
    Route::post('consulta-usuario', 'FrontControllerCompras@consultaUsuario');
});

I have a problem with a form in a blade template. I want to send the form to a post route like the one I put in the form and that I have created in the routes file. My form is;

{!! Form::open(['route' => 'compra.consulta-usuario', 'method' => 'POST']) !!}

The url of the form is: link

The error I get when loading the page is:

  

Route [purchase.consulta-user] not defined

    
asked by safra 13.10.2017 в 16:35
source

2 answers

0

The other answer partially solves the problem, but promotes a bad practice within Laravel: use URL instead of route names, so if you decide to change the url in the routes file, you will have to change it everywhere where the scribes (on the form in this case).

If you want to use the name of the route in the form (which I recommend), you just have to define the name of the same in the routes file:

Route::post('consulta-usuario', 'FrontControllerCompras@consultaUsuario')
    ->name('compra.consulta_usuario');

As for the other answer, the second method proposed (use Route::resource ) is incorrect in this case, since this syntax generates several basic routes but will not generate the name of the path compra.consulta_usuario .

    
answered by 13.10.2017 / 17:23
source
-1

Use:

{!! Form::open(['url' => 'compra/consulta-usuario', 'method' => 'POST']) !!}

If you want to use route you have to do the following:

In your web.php

Route::resource('compra', 'FrontControllerCompras')

In your FrontControllerCompras.php you have to create a method that is called consulta_usuarios

and now you can use

{!! Form::open(['route' => 'compra.consulta_usuario', 'method' => 'POST']) !!}

That should work for you ...

    
answered by 13.10.2017 в 16:42