<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ??一站式輕松地調用各大LLM模型接口,支持GPT4、智譜、豆包、星火、月之暗面及文生圖、文生視頻 廣告
                # 分頁 ### 基本用法 有幾種分頁記錄的方法,最常見的是`paginate`在查詢或模型上調用該方法。這將返回一個特殊的分頁集合,其中添加了額外的方法來顯示結果。 ### [](https://octobercms.com/docs/services/pagination#paginating-query-builder-results)分頁查詢構建器結果 有幾種分頁項目的方法。最簡單的`paginate`方法是使用[查詢構建器](https://octobercms.com/docs/database/query)上的方法或[模型查詢](https://octobercms.com/docs/database/model)。該`paginate`方法會自動根據用戶正在查看的當前頁面來設置適當的限制和偏移。默認情況下,當前頁面由`?page`HTTP請求上查詢字符串參數的值檢測。當然,該值將被自動檢測并自動插入到分頁器生成的鏈接中。 首先,讓我們看一下在`paginate`查詢上調用方法。在此示例中,傳遞給的唯一參數`paginate`是您希望“每頁”顯示的項目數。在這種情況下,我們指定要在`15`每頁顯示項目: ~~~ $users = Db::table('users')->paginate(15); ~~~ > **注意:**當前,使用`groupBy`語句的分頁操作無法有效執行。如果需要使用`groupBy`帶有分頁結果集的,建議您查詢數據庫并手動創建分頁器。 #### 簡單分頁 如果只需要在分頁視圖中顯示簡單的“下一個”和“上一個”鏈接,則可以選擇使用該`simplePaginate`方法來執行更有效的查詢。如果在渲染視圖時不需要為每個頁碼顯示鏈接,則這對于大型數據集非常有用: ~~~ $users = Db::table('users')->simplePaginate(15); ~~~ ### [](https://octobercms.com/docs/services/pagination#paginating-eloquent-results)分頁模型結果 您也可以分頁[數據庫模型](https://octobercms.com/docs/database/model)查詢。在此示例中,我們將在每頁項目上對`User`模型進行分頁`15`。如您所見,語法與分頁查詢構建器結果幾乎相同: ~~~ $users = User::paginate(15); ~~~ 當然,您可以`paginate`在對查詢設置其他約束(例如`where`子句)之后調用 ~~~ $users = User::where('votes', '>', 100)->paginate(15); ~~~ 您可以`simplePaginate`在分頁模型時使用該方法: ~~~ $users = User::where('votes', '>', 100)->simplePaginate(15); ~~~ 您可以通過傳遞第二個參數來手動指定頁碼,在此我們將`15`每頁的頁碼分頁,指定我們在頁面上`2`: ~~~ $users = User::where('votes', '>', 100)->paginate(15, 2); ~~~ ### [](https://octobercms.com/docs/services/pagination#manually-creating-a-paginator)手動創建分頁器 有時您可能希望手動創建一個分頁實例,并向其傳遞一系列項目。您可以根據需要通過創建一個`Illuminate\Pagination\Paginator`或`Illuminate\Pagination\LengthAwarePaginator`實例來實現。 本`Paginator`類并不需要知道結果集中項目的總數,因為這個類的沒有用于檢索的最后一頁的索引方法。在`LengthAwarePaginator`接受幾乎相同的參數`Paginator`,但它確實需要在結果集中的項目總數的計數。 換句話說,`Paginator`對應于`simplePaginate`查詢構建器和模型`LengthAwarePaginator`上的`paginate`方法,而對應于方法。 手動創建分頁器實例時,應手動“切片”傳遞給分頁器的結果數組。如果不確定如何執行此操作,請查看[array\_slice](http://php.net/manual/en/function.array-slice.php)PHP函數。 ### [](https://octobercms.com/docs/services/pagination#displaying-results-in-a-view)在視圖中顯示結果 當您在查詢構建器或模型查詢上調用`paginate`or`simplePaginate`方法時,您將收到一個分頁程序實例。調用該`paginate`方法時,您將收到的實例`Illuminate\Pagination\LengthAwarePaginator`。調用該`simplePaginate`方法時,您將收到的實例`Illuminate\Pagination\Paginator`。這些對象提供了幾種描述結果集的方法。除了這些輔助方法之外,分頁器實例是迭代器,并且可以作為數組循環。 因此,一旦檢索到結果,就可以顯示結果并使用Twig呈現頁面鏈接: ~~~ <div class="container"> {% for user in users %} {{ user.name }} {% endfor %} </div> {{ users.render|raw }} ~~~ 該`render`方法將呈現到結果集中其余頁面的鏈接。這些鏈接中的每個鏈接都已經包含正確的`?page`查詢字符串變量。該`render`方法生成的HTML與[Bootstrap CSS框架](https://getbootstrap.com/)兼容。 > **注意:**`render`從Twig模板調用方法時,請確保使用`|raw`過濾器,以免HTML鏈接被轉義。 #### 自定義分頁器URI 該`setPath`方法允許您自定義在生成鏈接時分頁程序使用的URI。例如,如果您希望分頁程序生成類似的鏈接`http://example.com/custom/url?page=N`,則應傳遞`custom/url`給`setPath`方法: ~~~ $users = User::paginate(15); $users->setPath('custom/url'); ~~~ #### 附加到分頁鏈接 您可以使用`appends`方法將查詢鏈接添加到分頁鏈接的字符串中。例如,要附加`&sort=votes`到每個分頁鏈接,您應該對進行以下調用`appends`: ~~~ {{ users.appends({sort: 'votes'}).render|raw }} ~~~ 如果您希望在分頁器的URL后面附加“散列片段”,則可以使用該`fragment`方法。例如,要附加`#foo`到每個分頁鏈接的末尾,請對該`fragment`方法進行以下調用: ~~~ {{ users.fragment('foo').render|raw }} ~~~ #### 其他輔助方法 您還可以通過以下方法在分頁程序實例上訪問其他分頁信息: ~~~ $results->count() $results->currentPage() $results->hasMorePages() $results->lastPage() (Not available when using simplePaginate) $results->nextPageUrl() $results->perPage() $results->previousPageUrl() $results->total() (Not available when using simplePaginate) $results->url($page) ~~~ ### [](https://octobercms.com/docs/services/pagination#converting-results-to-json)將結果轉換為JSON 分頁器結果類實現`Illuminate\Contracts\Support\JsonableInterface`協定并公開`toJson`方法,因此將分頁結果轉換為JSON非常容易。您還可以通過簡單地從路由或AJAX處理程序返回分頁程序實例將其轉換為JSON: ~~~ Route::get('users', function () { return User::paginate(); }); ~~~ 從分頁程序的JSON將包括元數據信息,例如`total`,`current_page`,`last_page`,和更多。實際結果對象將通過`data`JSON數組中的鍵提供。這是通過從路由返回分頁程序實例而創建的JSON的示例: #### 示例分頁器JSON ~~~ { "total": 50, "per_page": 15, "current_page": 1, "last_page": 4, "next_page_url": "http://octobercms.app?page=2", "prev_page_url": null, "from": 1, "to": 15, "data":[ { // Result Object }, { // Result Object } ] } ~~~
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看