2016-09-14 109 views
0

比方說,我有一個索引頁,其中顯示數據庫中的所有數據與分頁。我能夠在用戶執行搜索後顯示正確的數據和分頁,但是當我點擊第二頁時,它不顯示正確的數據。Laravel 5如何在一頁中使用多頁分頁

public function getNewsletterSearch(Request $request) { 
     $keyword = $request->news_search; 
     $searchData = DB::table('tbl_contents') 
       ->join('tbl_files', function ($join) use($keyword) { 
        $join->on('tbl_contents.unique', '=', 'tbl_files.unique') 
        ->where('tbl_contents.record_type', '=', 'Newsletter') 
        ->where('tbl_contents.title', 'LIKE', '%' . $keyword . '%') 
        ->where('tbl_files.order_id', '=', 1); 
       }) 
       ->orderBy('tbl_contents.id', 'DESC') 
       ->select('tbl_contents.*', 'tbl_files.file_name') 
       ->paginate(6); 

     $searchData->setPageName('page_result'); 

     return view('newsletter.index')->with(['searchData' => $searchData])->with('search', $keyword); 
    } 

這裏是視圖:

<div class = "col-xs-12"> 
      <nav aria-label = "Page navigation"> 
       {{with(new App\Pagination\CustomPresenter($searchData->appends('page_result', Illuminate\Support\Facades\Input::get('page_result'))))->render()}} 
      </nav> 
</div> 

這是我創建的搜索功能。我可以知道從搜索結果中顯示第2頁上的正確數據的解決方案是什麼?

回答

0

以下是我在搜索場景中使用的完整索引函數,請仔細閱讀並進行必要的修改。

public function getNewsletterSearch() 
{ 
    $keyword = request()->get('news_search'); 

    $searchData = DB::table('tbl_contents') 
     ->join('tbl_files', function ($join) use($keyword) { 
      $join->on('tbl_contents.unique', '=', 'tbl_files.unique') 
      ->where('tbl_contents.record_type', '=', 'Newsletter') 
      ->where('tbl_contents.title', 'LIKE', '%' . $keyword . '%') 
      ->where('tbl_files.order_id', '=', 1); 
     }) 
     ->orderBy('tbl_contents.id', 'DESC') 
     ->select('tbl_contents.*', 'tbl_files.file_name'); 

    $searchData= $searchData->paginate(6)->appends(request()->query()); 
    return view('category.index',compact('searchData')); 
} 
鑑於文件

<div class="col-sm-12 col-md-12"><span class="pull-right">{{$searchData->links()}}</span></div>