2 回答

TA貢獻1880條經(jīng)驗 獲得超4個贊
您可能需要執(zhí)行自定義解決方案:
public function emfoco()
{
if (request()->input('f', 1) == 1) {
$emfocoCount = Noticia::count();
$emfocoCollection = Noticia::orderBy('created_at','desc')->take(4);
$emfoco = new LengthAwarePaginator($emfocoCollection, $emfocoCount, 6, LengthAwarePaginator::resolveCurrentPage('f'), [ 'pageName' => 'f' ]);
} else {
$emfocoCount = Noticia::count();
$emfocoCollection = Noticia::orderBy('created_at','desc')
// If this is e.g. page 5 you skip the 4 on page 1 and the 18 on the 3 other previous pages
->skip(4+6*(LengthAwarePaginator::resolveCurrentPage('f')-2))->take(6);
$emfoco = new LengthAwarePaginator($emfocoCollection, $emfocoCount, 6, LengthAwarePaginator::resolveCurrentPage('f'), [ 'pageName' => 'f' ]);
}
$emfoco->setPath($request->getPathInfo()); // You might need this too
return view('em-foco')->with(['emfoco'=>$emfoco]);;
}
請注意,在這兩種情況下,分頁器都設(shè)置為假定每頁有 6 個結(jié)果,盡管事實上第 1 頁中只有 4 個結(jié)果。這是因為該數(shù)字基本上只是決定總頁數(shù)。(我認(rèn)為)這將導(dǎo)致計算出正確的總頁數(shù),盡管第一個結(jié)果將有 4 個結(jié)果(因為這就是我們要傳遞的所有結(jié)果)
更新:如果您有 6 個總結(jié)果,您可能會得到錯誤的頁數(shù),因此要修復(fù),您可以將$emfocoCount+2總結(jié)果數(shù)傳遞過去,以補償?shù)谝豁摰慕Y(jié)果比正常情況下少 2 個的事實。

TA貢獻1772條經(jīng)驗 獲得超6個贊
你可以嘗試一下:
public function emfoco()
{
if (!request()->get('f') || request()->get('f') == 1) {
$emfoco = Noticia::orderBy('created_at', 'desc')->paginate(4, null, 'f');
} else {
$emfoco = $emfoco->skip(4 + ((request()->get('f') - 2) * 6))->take(6);
$emfoco = new LengthAwarePaginator($emfoco->get(), Noticia::count(), 6, request()->get('f'));
}
return view('em-foco')->with(['emfoco' => $emfoco]);;
}
- 2 回答
- 0 關(guān)注
- 194 瀏覽
添加回答
舉報