簡體   English   中英

Laravel 中的分頁關系與 first

[英]Paginate relationship in Laravel with first

我遇到了一個問題,在 Laravel 中對關系進行分頁的最佳方法是什么?

例如:

$category = $this->blogCategory->whereId($categoryId)->with(['blogPosts'])->first();

在這里,我獲得了所有必需的數據,但是關系的分頁呢? 我已經通過first()剝離了類別以檢索第一個類別,但是如何對多個博客文章的morphToMany類型的關系blogPosts進行分頁? 實現這一目標的更好方法是什么?

編輯:

所以我在這個時候想出了這個。

        $category = $this->blogCategory->whereId($categoryId)->with(['blogPosts' => function ($query) {

            $query->paginate(1);
         
         }])->first();

雖然它不會打印出current_page ,但像limit一樣工作。

使用下面的代碼,它應該可以工作,因為它在 Laravel 5.4 中對我有用

$category = $this->blogCategory->whereId($categoryId)->first()->with(['blogPosts'])->paginate(1);

暫無
暫無

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

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