Maison012's avatar

It is okay to use POST method for pagination?

I have builded a table who manage some data passed from laravel. But i have too many data and of course i use pagination, also i have added a view per page. Every thing works but i dont feel very good with this method. I mean i use post method to pass view per page value with ajax couse my front end is vue js.

//laravel controller
 public function getUsers(Request $request)
    {
        $paginate = $request->perPage;

        return new UsersResource(User::paginate($paginate));
    }

//Route
Route::any('users/getusers', 'UsersController@getUsers')->name('permissions.getUsers');

and vue method

data() {
     return {
           displayRecord: 5,
                pagination: {
                    current_page: 1,
                },
                perPage: {
                    "10": "10",
                    "50": "50",
                    "100": "100",
                    "500": "500",
                },
         }
}
getUsers() {
                axios.post('/admin/users/getusers?page=' + this.pagination.current_page, {
                    perPage: this.displayRecord
                })
                .then(response => {
                    this.users = response.data.data
                    this.pagination = response.data.meta

I think there is a better way to make this, just i does not now how

0 likes
19 replies
Tray2's avatar

You can use whatever method you like, but it would be better to use a get, if you only fetch data from the server.

You can pass and object with data in a GET request as well,

Maison012's avatar

@Tray2 I know it is better to use get for fetching data, just does not know how to use in my case

Also i need to pass perPage value

Sinnbeck's avatar

@Leon012 Cant you just change the route to get and do

 axios.get('/admin/users/getusers', {
                 params: {
                    perPage: this.displayRecord,
                   page:  this.pagination.current_page
                }
               })
Maison012's avatar

@Sinnbeck I have tryed but pagination does not work. Page per view work but pagination not

links: {first: "http://127.0.0.1:8000/getusers?page=1", last: "http://127.0.0.1:8000/getusers?page=1",…}
first
: "http://127.0.0.1:8000/getusers?page=1"
last : "http://127.0.0.1:8000/getusers?page=1"
next: null
prev: null
Sinnbeck's avatar

@Leon012 Seems you arent returning the data. I assume UsersResource is a json resource. So the syntax is

UsersResource::collection(User::paginate($paginate));
Maison012's avatar

@Sinnbeck Yes UsersResource return json formant. I just tryed

UsersResource::collection(User::paginate($paginate));
``` 
but does not work. SHow me same responds 
Maison012's avatar

@Sinnbeck

<?php

namespace App\Http\Resources;

use Illuminate\Http\Resources\Json\JsonResource;

class UsersResource extends JsonResource
{
    /**
     * Transform the resource into an array.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return array|\Illuminate\Contracts\Support\Arrayable|\JsonSerializable
     */
    public function toArray($request)
    {
        return parent::toArray($request);
    }
}

Sinnbeck's avatar

@Leon012 Ah ok so it does nothing :) Do you get data if you just return the users paginated directly?

        return User::paginate($paginate);
Maison012's avatar

@Sinnbeck It is same response, nothing change. But on network tab i get some data in both cases

Maison012's avatar

@Sinnbeck Yes i know. I had used before collection, when i use post method for showing pagination.

But now i am trying to use get method and does not understand why is not working

Sinnbeck's avatar

@Leon012 Get it working as you want by opening the url directly in the browser. When it does, convert it to axios. I gave an example above

1 like
Maison012's avatar
Maison012
OP
Best Answer
Level 4

@Sinnbeck I have changed to something like this

 axios.get('/getusers', {
                    params: {
                        'perPage': this.displayRecord,
                        'page': this.pagination.current_page
                    }
                })

and using resource collection it work for me.

Get it working as you want by opening the url directly in the browser.

I see in browser but was not able to see on table, and problem was i was working with collection before and should change some things if i switch to json.

But now it work for me. Thank you

Please or to participate in this conversation.