When method changed to GET route not working like POST method Laravel

Viewed 158

I have a search bar in view:

{!! Form::open(['name' => 'myForm', 
                'method' => 'GET', 
                'action' => 'AreaController@search',
                'files' => true, 
                'onsubmit' => "return validateForm()"]) 
!!}
         
    {!! Form::submit('جستجو', ['class' => 'btn btn-info']) !!}
                       
{!! Form::close() !!}

And in route file, web.php:

Route::get('area/search/', 'AreaController@search')->name('area.search');

AreaController:

public function search(Request $request) {
    return " it is working" ;
}

But when I click on the button, the browser is showing a blank page. When I use POST method it is working, but if I change to GET method, it is not working.

Thank you.

1 Answers

For Post Method, you need to add CSRF Token To A Form


{!! Form::open(['method' => 'POST']) !!}  <--------- Change to POST method
    
{!! Form::token() !!}  <----------- Add this line 


    {!! Form::submit('جستجو', ['class' => 'btn btn-info']) !!}
                       
{!! Form::close() !!}


Route::post('area/search/', 'areacontroller@search')->name('area.search');

OR

If you don't want to add CSRF Token To A Form, You can add that to the route file.

Attaching The CSRF Filter To A Route


Route::post('profile', array('before' => 'csrf', function()
{
    //
}));

For More Details, you can refer to https://laravel.com/docs/4.2/html

Related