duc91bk's avatar

Search Results Paginate

I creat pagination:

Route::post('search',[ 'as'=>'search.post', 'uses' => 'SearchController@getSearchPost' ]);

class SearchController extends Controller {

public function getSearchPost(Request $request)
{

    $key=$request->key;

    $articles = Article
        ::orderBy('created_at','desc')
        ->where('title', 'like', "%$key%")
        ->select('articles.*')
        ->paginate(20);

    return view('articles.search',[ 'articles'=>$articles,'key'=>$key ]);
}

}

blade: {!! $articles->appends(['title'=>$key])->render() !!}

whe i click like page 2(http://localhost:8000/search/?title=t&page=2) then receive empty page. who showed me how to resolve

0 likes
10 replies
thomaskim's avatar

@duc91bk That's because you should be using GET requests for Laravel's pagination, but your route requires a POST request.

duc91bk's avatar

I use POST because i sent the search form

thomaskim's avatar

A lot of people use GET requests for search forms, and actually, a GET request would probably be preferred. Take a look at Google.

duc91bk's avatar

when i use GET then receive error:MethodNotAllowedHttpException in RouteCollection.php line 201:

thomaskim's avatar

If you haven't already, you would need to change your routes file so that it accepts a GET request. Then, you would need to change your form to perform a GET request rather than a POST.

duc91bk's avatar

i try change to Form::open([ 'route'=>['search.post'], 'method'=>'GET', 'class'=>'search_form' ]) and change route::get then i click link page:2 result wrong

baldaweb's avatar

add this line $articles->setpath('');

...
$articles->setpath('');
return view('articles.search',[ 'articles'=>$articles,'key'=>$key ]);
pmall's avatar

Always use get for serach forms. Then your code above should work.

Please or to participate in this conversation.