The second, you need to append query string I suspect. Refer to docs under pagination.
Laravel Pagination causes MethodNotAllowedHttpException ?
So I am working on a site that paginates two different types of DB queries.
The first is a generic "get everything" query. The second has specific checks (e.g "WHERE name = 'John'")
Both are done with simplePaginate(num); I'll show my code if it's considered relevant.
The first works perfectly fine. BUT the second throws an error whenever I use the Next or Previous buttons to fetch the next results.
Symfony \ Component \ HttpKernel \ Exception \ MethodNotAllowedHttpException
No message
This is not an out-of-bounds error since I know the database has more values under the specified query. **EDIT: No longer the case, see "EDIT 2". **
Does anyone know why this happens?
EDIT: For clarity, here is my code for the second query. It works for the first page, but ONLY causes errors when I use one of the buttons to move to another page.
$postmen = DB::table('postmen')
->join('cats', 'postmen.cat_id', '=', 'cats.cat_id')
->join('reg_status', 'postmen.status_id', '=', 'reg_status.status_id')
->select('postmen.postmen_id', 'postmen.forename', 'postmen.surname',
'cats.description AS catdesc', 'reg_status.description AS regdesc')
->where(function ($query) use ($request) {
if (!empty($request->forename)) {
$query->where('postmen.forename', $request->forename);
}
if (!empty($request->surname)) {
$query->where('postmen.surname', $request->surname);
}
if (!empty($request->cat_id)) {
$query->where('postmen.cat_id', $request->cat_id);
}
if (!empty($request->status_id)) {
$query->where('postmen.status_id', $request->status_id);
}
if (!empty($request->postmen_id)) {
$query->where('postmen.postmen_id', $request->postmen_id);
}
})
->paginate(10);
EDIT 2: It's been pointed out that now the route was incorrect. I had been using exclusively "route::post". I have added a route::get for my view now:
Route::get('/postmen', 'myController@searchByCriteria');
I am no longer getting a "MethodNotAllowedHttpException". Instead I am getting a time-out because it's redirecting itself too often. I'm not sure what's wrong there.
Any advice?
You should try this:
Yes pagination only works with get parameters.
You should use GET method for your search page. POST requests aren't meant for the purpose of displaying data. Why? There are many reasons, but to be short I will give you two examples:
With GET parameters, let's say you are on sixth page - you can copy the link and paste it to friend and he will be able to view the same content as you. With POST this is impossible.
You can not use back button with POST requests, if you manage to get pagination to work.
POST requests are useful when you need to submit data to the server, in order to create new record, for example.
So I suggest you to change your route type to GET and your search form method to GET.
Please or to participate in this conversation.
