Naysoewin's avatar

Load more instead of pagination

Hello everyone,

Currently, i am working on React and Laravel project , I just would like to achieve 'load more' instead of pagination ,

Actually i have pretty simple index method that implementing with spatie query builder.


 public function index()
    {
        $products = QueryBuilder::for(Product::class)
            ->allowedFilters([
                'subject',
                AllowedFilter::exact('user_id'),
            ])
            ->withCount(['viewCounts'])
            ->paginate(20);

        return (new ProductResource($products))
            ->additional([
                'message' => 'success'
            ])
            ->response()
            ->setStatusCode(Response::HTTP_OK);;
    }

public function loadMore() {
	//
}

how should i get 20 news products when i send get request from client with above query builder options

Thanks

0 likes
1 reply
bugsysha's avatar

Initial response is stored in some array on the React component. You remember what the next URL is from initial paginated response and when you load more, then just append to that same array you have on the React component new data you got from response. Nothing complicated.

Please or to participate in this conversation.