【问题标题】:how to show pagination links in resource laravel如何在资源 laravel 中显示分页链接
【发布时间】:2021-12-08 10:08:46
【问题描述】:

我在这个资源中有一个资源我有收藏,现在我想显示那个收藏的分页链接,我该怎么做?

这是我的资源

public function toArray($request)
{


    return [
        "name"              =>  $this->name ,
        "slug"              =>  $this->slug ,
        "bg_image"          =>  imageGenerate("sections" , $this->bg_image) ,
        "bg_color"          =>  $this->bg_color ,
        "items"             =>  new ItemCollection($this->items()->paginate(20)) ,
    ];
}

这是我的收藏

public function toArray($request)
{

    return $this->collection->map(function ($item) {
        return [
            "id"            =>  $item->id ,
            "name"          =>  $item->name ,
            "slug"          =>  $item->slug ,
            "image"         =>  imageGenerate("items" , $item->image) ,
            "code"          =>  $item->code ,
            "category"      =>  $item->category->name??""
        ];
    });
}

【问题讨论】:

    标签: php laravel collections pagination resources


    【解决方案1】:

    根据 Laravel 8.x 文档,您可以将要分页的数据传递给新创建的 Collection 实例。这是一个例子:

    use App\Http\Resources\UserCollection;
    use App\Models\User;
    
    Route::get('/users', function () {
        return new UserCollection(User::paginate());
    });
    

    另见this

    【讨论】:

      猜你喜欢
      • 2019-09-05
      • 2016-09-15
      • 2021-12-16
      • 2022-01-18
      • 1970-01-01
      • 2017-12-23
      • 2020-05-27
      • 1970-01-01
      • 2021-01-10
      相关资源
      最近更新 更多