ホームページ >バックエンド開発 >PHPチュートリアル >Laravelの微調整ページネーションリンク
LaravelのonEachSide
メソッドは、現在のページの近くに表示されるページネーションリンクに対するきめ細かい制御を提供し、ナビゲーションの明確さを強化します。 デフォルトのリンクカウントに依存する代わりに、アプリケーションの特定のニーズに合わせてディスプレイを調整できます。
適応ページネーションを実装する方法:
ブレードテンプレート(
<?php namespace AppHttpControllers; use AppModelsArticle; use IlluminateHttpRequest; class ArticleController extends Controller { public function index(Request $request) { $perPage = $this->determinePerPage($request); $articles = Article::latest() ->withCount('comments') ->paginate($perPage); $linksToShow = $this->calculateVisibleLinks($articles); return view('articles.index', compact('articles', 'linksToShow')); } private function determinePerPage(Request $request): int { return $request->query('show', 10); } private function calculateVisibleLinks($paginator): int { $totalPages = $paginator->lastPage(); // Dynamically adjust visible links based on total pages return match(true) { $totalPages > 20 => 3, $totalPages > 10 => 2, default => 1 }; } }):
articles.index.blade.php
{{ $articles->onEachSide($linksToShow)->links() }}
動的リンク計算と組み合わせて、
<code>// ~50 articles (5 pages) « 1 2 3 4 5 » // ~150 articles (15 pages) « 1 ... 4 5 6 7 8 ... 15 » // ~250 articles (25 pages) « 1 ... 4 5 6 7 8 9 10 ... 25 »</code>メソッドは、よりユーザーフレンドリーで適応性のあるページネーションエクスペリエンスを作成します。
以上がLaravelの微調整ページネーションリンクの詳細内容です。詳細については、PHP 中国語 Web サイトの他の関連記事を参照してください。