Paginate / Search Laravel issue

半世苍凉 提交于 2019-12-13 04:37:18

问题


After a couple of conversations on here i'm finally getting somewhere but after the pagintion now works when i got to search?page=2 i get a json array of the logged in user and not page 2 of the results.

Here's my controller:

  public function search() 
  {
  $q = Input::get('term');
  if($q && $q != ''){
    $searchTerms = explode(' ', $q);
    $query = DB::table('wc_program');

    if(!empty($searchTerms)){

      foreach($searchTerms as $term) {
        $query->where('JobRef', 'LIKE', '%'. $term .'%');
        $query->orwhere('Road', 'LIKE', '%'. $term .'%');
      }
    }
    $results = $query->paginate(10);


    return View::make('layouts.results', compact('results'));
   }
}

Route: Route::get('/search', 'HomeController@search');

So how can i work around this?


回答1:


You need to append query string to the pagination.

in your controller, pass the query string to the view.

return View::make('layouts.results', compact('results', 'q'));

In your view (results.blade.php):

append the query string to the pagination otherwise in page 2 you will not get any result.

<?php echo $results->appends(array('term' => $q))->links(); ?>


来源:https://stackoverflow.com/questions/20961006/paginate-search-laravel-issue

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