簡體   English   中英

如何自定義此 Laravel 8 應用程序中的分頁項?

[英]How do I customize the pagination items in this Laravel 8 application?

我正在開發 Laravel 8 中的博客應用程序

ArticlesController controller 我有這個方法來顯示分頁文章:

namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Models\User;
use App\Models\ArticleCategory;
use App\Models\Article;
use App\Models\Comment;

class ArticlesController extends FrontendController {

    // Articles per page
    protected $per_page = 12;

    public function index(Request $request) {

        // Search query
        $qry = $request->input('search');

        $articlesQuery = Article::where('title', 'like', '%' . $qry . '%')
                                            ->orWhere('short_description', 'like', '%' . $qry . '%')
                                            ->orWhere('content', 'like', '%' . $qry . '%');

        // Search results count
        if ($qry) {
            $article_count = $articlesQuery->count();
        }   

        $articles = $articlesQuery->orderBy('id', 'desc')->paginate($this->per_page);   
        $featured_articles = Article::where('featured', 1)->orderBy('id', 'desc')->get();

        return view('themes/' . $this->theme_directory . '/templates/index', 
            array_merge($this->data, [
                'search_query' => $qry,
                'articles' => $articles,
                'featured_articles' => $featured_articles,
                'article_count' => $article_count ?? null
            ])
        );
    }
    
}

分頁,在普通的 HTML模板中,如下所示:

<nav class="pgn">
  <ul>
      <li>
        <a class="pgn__prev" href="#0">Prev</a>
      </li>
      <li><a class="pgn__num" href="#0">1</a></li>
      <li><span class="pgn__num current">2</span></li>
      <li><a class="pgn__num" href="#0">3</a></li>
      <li><span class="pgn__num dots">…</span></li>
      <li><a class="pgn__num" href="#0">4</a></li>
      <li>
         <a class="pgn__next" href="#0">Next</a>
      </li>
  </ul>
</nav>

目標

目標是在 Laravel 的 Blade 中保持上面的分頁結構。

問題

下面的代碼適用於“Next”和“Prev”按鈕,但不適用於它們之間的鏈接。

<nav class="pgn">
  <ul>
      <li>
        <a class="pgn__prev" href="{{ $articles->withQueryString()->previousPageUrl() }}">Prev</a>
      </li>

        {!! $articles->withQueryString()->links() !!}

      <li>
         <a class="pgn__next" href="{{ $articles->withQueryString()->nextPageUrl() }}">Next</a>
      </li>
  </ul>
</nav>

問題

  1. 是什么導致了這個錯誤?
  2. 最簡單的解決方法是什么?

要創建自定義分頁,我建議使用自定義“視圖”。

基本上,您需要做什么(您已經完成了),就是定義一個限制,然后基本上執行以下操作:

您創建您的視圖文件(這將是自定義分頁器) - 將其命名為您想要的名稱。 我將其命名為custom.blade.php

此視圖必須在運行命令后創建: php artisan vendor:publish --tag=laravel-pagination

在這里您可以在有關它的文檔中找到更多信息: https://laravel.com/docs/9.x/pagination#customizing-the-pagination-view

@if ($paginator->onFirstPage())
    <li class="disabled"><span>← Previous</span></li>
        @else
            <li>
                <a class="pgn__prev" href="{{ $articles->withQueryString()->previousPageUrl() }}">Prev</a>
            </li>
        @endif


      
        @foreach ($elements as $element)
           
            @if (is_string($element))
                <li class="disabled"><span>{{ $element }}</span></li>
            @endif


           
            @if (is_array($element))
                @foreach ($element as $page => $url)
                    @if ($page == $paginator->currentPage())
                        <li class="active my-active"><span>{{ $page }}</span></li>
                    @else
                        <li><a href="{{ $url }}">{{ $page }}</a></li>
                    @endif
                @endforeach
            @endif
        @endforeach


        
        @if ($paginator->hasMorePages())
            <li>
                <a class="pgn__next" href="{{ $articles->withQueryString()->nextPageUrl() }}">Next</a>
            </li>
        @else
    <li class="disabled"><span>Next</span></li>
@endif

最后,在您要使用自定義分頁的視圖上:

{{ $articles->links(‘path.pagination.custom') }}

(只要確保有正確的路徑)

編輯:如果不看項目本身,我很難給出一個明確的答案,但我希望這至少能有所幫助。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM