我在控制器上使用 paginate() 將資料傳遞給視圖。這是我使用 paginate() 的索引函式
$userData = User::with('jobUnit')
->select('nip','name','address','phone','email','unit_id')
->paginate(10);
return view('users.index', [
'users' => $userData
]);
這是結果:

在另一個函式中,我需要在查詢中添加一些 IF 條件,如下所示:
$keyword = $request->keyword;
$searchedData = User::with('jobUnit')->select('nip','name','address','phone','email','unit_id');
if ($request->searchFilter == 'nip') {
$searchedData->where('nip','like','%'.$keyword.'%');
}
$searchedData->paginate(10);
結果不同,這對我來說是個問題,因為我在視圖中使用了分頁鏈接。結果如下:

分頁()不起作用嗎?因為我也嘗試使用 get() 應該回傳“Collection”,但它仍然回傳“Builder”結果。
uj5u.com熱心網友回復:
您需要另一個變數來存盤回傳資料
$searchDataPaginated = $searchedData->paginate(10);
或根據需要使用當前的
$searchedData = $searchedData->paginate(10);
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/510082.html
標籤:php拉拉维尔雄辩
