我的自定义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个回答

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

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问