0
votes

project is laravel 5.6. My project has 2 routes:

web.php

Route::get('testa', 'HomeController@showTestForm')->name('test');
Route::post('testa', 'HomeController@doTest');

HomeController :

public function showTestForm()
{
    Log::warning('from showTestForm');    
    return view('test');
}

.public function doTest(Request $request)
{
    Log::info('from doTest');    
    // return Input::all();
    return view('test', [
        'input' => implode(', ', Input::all()),
    ]);
}

test.blade.php

<form method="post" action="{{ route('test') }}">
@csrf

<input type="text" name="inputvalue">
<button type="submit" class="btn btn-primary">
merge
</button>

</form>

<div>Result</div>
@if(isset($input))
{{$input}}
@endif

Why is working post on route('test') ? Thank you.

4
Your form's method is POST, so it should send a POST request and not get. If you'll change the form method to get you'll trigger the Route::get() method.AfikDeri
Ok, but why is working post form on get route?nipuro

4 Answers

0
votes

Edit:

Route::post('testa', 'HomeController@doTest')->name('postTest');

and then use

<form method="post" action="{{ route('postTest') }}">

Hope this will work

0
votes

It is because you'are calling the wrong route.

Change : <form method="post" action="{{ route('test') }}">

to : <form method="post" action="{{ url('/testa') }}">

or follow the previous answer steps (name the post route then call it)

0
votes

I believe from the OP they are not understanding how the same route can accept both a GET request and a POST request.

The difference is in how the server receives the data.

Have a read: HTTP Requests

0
votes

The reason that route('test') works even though your form is a POST request is because route() is just a helper function to generate a url and your GET and POST routes both use the same url.

You've specified in your form to make a post request and it's going to send it to the url provided (which will be the same as your GET request in this case).