dongxianchu3541 2019-04-22 00:11
浏览 813

我的自定义laravel paginate方法的特征/范围

I want to implement my own pagination for vue and laravel, instead of using get like laravel I want to make use POST ajax requests from my custom vue pagination component.

For that I want to implement something similar to a scope I can apply to my Eloquent models, I want that scope to return the total number of records from my model in database and a specific number of records along with it.

With this two data I can hydrate my pagination component, since I'm not really sure how to approach this (use Trait?, a scope?, return JSON? or collection maybe?) I leave my pseudocode bellow:

public function scopeGetPaginate($query, $perPage)
    {

        $pageResults = $query->take($perPage)->get();

        $totalResults = Model::count();

        return response()->json([
            'pageResults' => $pageResults,
            'totalResults' => totalResults,
        ], 200);
    }
  • 写回答

1条回答 默认 最新

  • douhaoqiao9304 2019-04-22 04:56
    关注

    I cant understand why are you going to implement custom pagination instead of using Laravel Paginate option.like this

    $users = User::where('votes', '>', 100)->paginate(15);
    

    Laravel Pagination Document

    However if you really want to create custom pagination your can refer Laravel Pagination Api to get an idea about pagination class.

    Laravel Paginator API Reference

    评论

报告相同问题?

悬赏问题

  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥15 请问Lammps做复合材料拉伸模拟,应力应变曲线问题
  • ¥30 python代码,帮调试
  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿
  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥100 Jenkins自动化部署—悬赏100元