Pagination in Laravel 4 works for one page. but not working for another

穿精又带淫゛_ 提交于 2019-12-12 10:23:55

问题


I downloaded the starter kit for laravel using sentry from GitHub.

Everything is fine so far. Blog page has pagination. I am able to change the number of items per page as I like by changing the same pagination(x) value.

Now problem is, i created a new page manually by myself called "Search" which displays results for Keywords searched.

Here I added the same pagination, it shows the number of items per page as mentioned, page numbers, arrows. Everything is perfect, but when I click page 2 it shows a BLANK PAGE. The page source of blank page is also empty. The URL is perfect for page two. It generates correctly as

sitename.dev/search?page=2

Please guide me where I am going wrong. The blog page pagination still works fine. Problem occurs only in newly created SEARCH page pagination.

This is my Controller

public function postSearch()
{
    $searchString = Input::get('searchInput');

    $posts = Post::where('title', 'LIKE', '%' . $searchString . '%')->orderBy('created_at', 'DESC')->paginate(4);

    if($searchString){

    return View::make('frontend/search', compact('posts'))->with('success', 'Account successfully updated')->with('posts', $posts);
    }
    else{
        return Redirect::route('home')->with('error', 'Please enter Search Term');
    }

}

This is my Route

Route::any('/search', 'BlogController@postSearch');

回答1:


First of all

return View::make('frontend/search', compact('posts'))->with('success', 'Account successfully updated')->with('posts', $posts);

When doing compact you're already including the $posts var to be used as $posts in the view, exactly the same you'd do using with('posts', $posts). So this last can be removed. If you want to load more vars using compact just separe the variable names by commas.

You're paginating the item in the POST action of Search, when you click any of the pagination links or call page=2 you're not running the postSearch method, you're probably calling getSearch as you're not doing now a POST request.

Have a look to RESTful controllers




回答2:


Search form better using GET method, because it's make easy to bookmark/save the URL for later use. In Laravel 4's Pagination, there's a method to appends the query string. So, we can keep the search querystring in other pages.

Docs: http://laravel.com/docs/pagination#appending-to-pagination-links

My code:

Search Form

<form method="get" action="{{{ URL::to('lib/search') }}}">
<input class="input-xxlarge" name="q" type="text" placeholder="Search...">
<div class="control-group">
    <div class="controls">
        <input type="submit" class="btn" id="submit" value="Submit" />
    </div>
</div>

Routing

Route::get('lib/search', 'LibraryController@getSearch');

Controller

public function getSearch()
{
    $search = Input::get('q');

    $posts = $this->post->where('title', 'like', '%'.$search.'%')->paginate(10);

    return View::make('site/libraries/list', compact('posts', 'search'));
}   

View / Blade At method to display pagination:

{{ $posts->appends(array('q' => $search))->links() }}

So we can keep the q=? querystring in other pages (ex: /lib/search?page=2&q=apple)




回答3:


A few things to check.

  1. The new route exists in the app/routes.php file.
  2. You are returning a view file for the new route.
  3. That view file has the pagination output. For example: <?php echo $articles->links(); ?>.

Ultimately, there appears to be some kind of error occurring, which is why the page is rendering blank. Trying turning php errors on in your dev env and see what happens after that.




回答4:


appends solved my issue. Also usage of appends will enable our users to save/bookmark the URL for future use.

But one problem is, it is not appending First page of the result.



来源:https://stackoverflow.com/questions/17451963/pagination-in-laravel-4-works-for-one-page-but-not-working-for-another

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!