Laravel search function crashes after page refresh

I have a simple search function

public function search(Request $request)
{
    $posts = Post::where('title', 'like', '%'.$request->term.'%')->paginate(10);
    return view('posts.search', compact('posts'));
}

My route:

Route::group(['prefix' => 'posts', 'as' => 'posts.'], function () {
    Route::post('/search', ['as' => 'posts', 'uses' => 'SearchController@posts']);
});

And my opinion:

            <form action="{{ route('searches.posts') }}" method="post">
                {{ csrf_field() }}
                <div class="input-field col s6">
                    <input class="input blue-text text-lighten-3" type="text" name="term">
                </div>
                <div class="input-field col s6">
                    <button type="submit">
                        <i class="material-icons">search</i>
                    </button>
                </div>
            </form>

I call this function after sending and receiving a new view with all the elements related to the search. So far this works, but if I refresh the page, I get the following error:

Symfony \ Component \ HttpKernel \ Exception \ MethodNotAllowedHttpException No message

+4
source share
2 answers

Your form method post, so in your initial request, the HTTP method is POST. When you refresh the page, the browser makes a GET request, not a POST request.

Make sure your route is registered for GET and POST requests:

Route::match(['get', 'post'], '/search', ['as' => 'posts', 'uses' => 'SearchController@search']);

https://laravel.com/docs/5.5/routing#basic-routing

get. , URL- .

+1

. , :

'uses' => 'SearchController@snippets'

To:

'uses' => 'SearchController@search'
+1

Source: https://habr.com/ru/post/1692496/


All Articles