beracah.kings's avatar

pagination

im posting values to a controller function and getting results with paginate. pagination doesnt work with post request. how to resolve.

0 likes
19 replies
Sinnbeck's avatar

Post is for saving data not retrieving data. Are you using ajax or regular forms?

Sergiu17's avatar

@beracah.kings depends on how you build your pagination links, but, all you need to do is to send page parameter to the request

curl -d "page=1" -X POST http://localhost:8000/users
Sinnbeck's avatar

Why does the route need to be POST? A GET request would make more sense if you are simply getting paginated data based on some values.

2 likes
beracah.kings's avatar

im collecting data from a form and then submitting to page, after some updates, the data is sent back. i m submitting 10 fields, so will that be odd to pass the parameters in get. can it be done through redirect

Sinnbeck's avatar

Yes but you will then need to set the page number yourself like @sergiu17 said (unless it should just be page 1)

beracah.kings's avatar

thank you sinnbeck. if i pass values through querystring, only the first page display displays correctly, the second pagination link fetches all the data. What could be wrong.

beracah.kings's avatar
        $gender = $request->input('gender');
        $mothertongue  = $request->input('mothertongue');
        $maritalstatus  = $request->input('maritalstatus');
        $caste  = $request->input('caste');
        $denomination  = $request->input('denomination');


        $partnerprofile2=new \App\partnerprofile;

        $partnerprofile=$partnerprofile2 
        ->when($gender, function($query, $gender) {
         $query->where('gender', $gender);
        })->when($mothertongue, function($query, $mothertongue) {
            $query->where('mothertongue', $mothertongue);
        })->when($maritalstatus, function($query, $maritalstatus) {
            $query->where('maritalstatus', $maritalstatus);
        })->when($caste, function($query, $caste) {
            $query->where('caste', $caste);
        })->when($denomination, function($query, $denomination) {
            $query->where('denomination', $denomination);
        })            
        ->paginate(10);   
        return view('search.response')->with(['partnerprofile'=>$partnerprofile]);


Sinnbeck's avatar

Is that a post or get route? And are you sure page is set? Check $request->input('page');

beracah.kings's avatar

@sinnbeck if the values are in the query string, then pagination doesnt work. Only if the values are set inside the controller action then pagination works.

Sinnbeck's avatar

You can just pass the page manually if it does not work

->paginate(10, ['*'], 'page',$request->page);
beracah.kings's avatar
beracah.kings
OP
Best Answer
Level 3

This answer worked for me

{{ $partnerprofile->appends(request()->except('page'))->links() }}

Im appending the querystring into the url . Now its working

Sinnbeck's avatar

Great. Remember to mark your answer as best

beracah.kings's avatar

do we have to sanitize the input, im using eloquent for data retrieval

Sinnbeck's avatar

Yes always sanitize your inputs. Users are evil (well some are)

alan9608's avatar

This is the most under explained feature of Laravel I have come across so far. There are mysterious variables ($paginator) that is used in the views/vendor/pagination/bootstrap-4.blade.php, ($elements) that throws an error , no explanation of what file these bits are supposed to be, what they represent, what file they are used inside of, no explaination of anything!

I have admittedly been trying to bite off more than I an chew by also including Vue components in the mix but that is another issue altogether.

I have the correct data coming back (FINALLY) from the api route where the pagination links and meta are correct and accounted for. My list of items is correctly displayed in a Vue component. If I add ANY extra piece to the mix in any file at ANY time, the navbar.vue stops functioning without explaination, the item list never fires and there is no console error or warnig. Just NOTHING. SO frustrating!

I know that some fellow egg head out there will say "show me the code else I can't understand". Well none of the code works! It is absent when the page works and nothing is there when I add it. I just need to know where therse variables are first mentioned, what they represent, what file they are defined in and what visibility they are supposed to have.

Please or to participate in this conversation.