Gabotronix's avatar

Trait/Scope for my custom laravel paginate method

Hi everybody, 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);
    }
0 likes
1 reply
MaverickChan's avatar
public function scopeGetPaginate($query) 

    {

        return $this->paginate(request('limit',20));


    }

in your controller

public function fetchdata()

{

    return response()->json([

        'collection' => Model::GetPaginate();

    ]);

}

Please or to participate in this conversation.