Route and form in Laravel

1

I have a route

Route::get('/','WebController@index');

I have a controller

public function index()
{
    $cars = Cars::table('cars')->get();
    return view('web::index')->with('cars', $cars);
}

And in form it passes the following URL

{!! Form::open(['class' => 'form-horizontal search-side-box', 'url' => '/', 'method' => 'post', 'autocomplete' => 'off']) !!}

But when I click the button submit of form , it gives the following error:

Class App\Front\Http\Controllers\WebController does not exist

But the controller is configured so much that in other places I call the URL '/' and it works, only in form that does not. And when I pass another URL in form it also works:

{!! Form::open(['class' => 'form-horizontal search-side-box', 'url' => '/about', 'method' => 'post', 'autocomplete' => 'off']) !!}
    
asked by anonymous 08.12.2017 / 16:12

1 answer

2

Good first your method in the Form and the routes is wrong, in one is defined a post and in the other the get. Usually get is used for simple pages, and the post in submission of forms and the like, in your case I think that is missing some code there:

Set another route

Route::get('/', 'WebController@index'); // renderiza a pagina com o form
Route::post('/busca_detalhe', ['as'=>'car.busca_detalhe', 'uses'=> 'WebController@busca_detalhe']); // Retornará os resultados submetidos na busca do form

Add a function in WebController

public function busca_detalhe(Request $request)
{
    $dados = $request->except('_token');
    $cars = DB::table('cars')->select('*')->where('parametro', '=', $dado['parametro'])->->get();
    return view('car.detalhes', compact('cars')); //Crie a view para mostrar os resultados
}

You can also set the opening of the Form with the route instead of the url:

{!!  Form::open(['route' => 'car.busca_detalhe', 'method' => 'POST', 'class'=>'form-horizontal bordered-row']) !!}  

Do not try to do everything in the same function / Controller using the same routes, this will confuse you at the time of maintenance, and need not quote if another developer will analyze your code one day ...

    
08.12.2017 / 16:30