Terrorizer's avatar

Laravel 4 pagination links return empty pages

I am trying to paginate thousands of results from database using Laravel 4.2 Unfortunately the page shows page numbers, which after clicking just refresh page leaving only get parameter in my link "?page=2". Is there anything that I am missing?

Controller

$data = SavedValues::where('h_user', '=', $user->id)->orderBy('id', 'desc')->paginate(100);

return View::make('saved-values-view')->with('data', $data);

View:

            @foreach($data as $d)
                <tr>
                    <td>{{ $d->datetime }}</td>
                    <td>{{ $d->value }}</td>
                </tr>
            @endforeach

            {{$data->links()}}
0 likes
11 replies
jlrdw's avatar

Is there a query string to append. And can you narrow results so you are not paging so many.

Terrorizer's avatar

Well, no matter how many records im trying to show, it doesnt work. Thousands of records was just an example actually. Not sure what you mean by query string to append. Im just entering user's id and trying to get my DB records. They display properly on the first page, or if I dont paginate them at all

jlrdw's avatar

How many results do you get if you dd the above.

Everything looks correct.

Terrorizer's avatar

It shows the first 100, other pages just reload the view with no results or anything. There is 1700 records i am trying to reach for examle searching by one of the users id

jlrdw's avatar

So the dd shows the query is correct, And if I understood correctly the first page results show, you just can't get to page 2 3 4 etc.

Is that part correct the first page in the view does show the data just nothing after that?

Your code looks correct. Still scratching my head.

Terrorizer's avatar

Everything is perfect until i press pages 2,3,4... Thank you for trying anyway :D

jlrdw's avatar

Append the user id to query string, is the only thing I can think of.

Terrorizer's avatar

In other words should i just enter string instead of variable to the db query or i understand it wrong?

jlrdw's avatar

Give me a couple of minutes, booting laptop, was on mobile.

Show all of your controller code, that method.

jlrdw's avatar

I have studied over this several times, it should be working as is, but you do not show all your code, model, whole controller method, etc.

It has to be something with $user->id

But below works if coming from a request.

I tried, but I no longer have version 4.2

$id = Input::get('id');
or
$id = !empty(Input::get('id')) ? Input::get('id') : some default value you want to set;
or
if (Input::has('id'))
{
    do your thing
}
// first request I imagine comes from a form request, I have no idea on your setup.
// seems it would come from somewhere.
// anyway, however you choose get the id

then

You need your query as above:

$data = SavedValues::where('h_user', '=', $id)->orderBy('id', 'desc')->paginate(100);

Then

$params = array('h_user' => $id);

or

$params = ['h_user' => $id];

Then

return View::make('saved-values-view')->with('data', $data)>with('params', $params);

or

return View::make('saved-values-view', compact( data', 'params'));

View:

{{ $data->appends($params)->links() }}

Since you have the where clause. You have to request that id each time.

Don't forget to put use statement at top for request.

I forget if laravel 4.2 needed it, 5.7 does.

but read

https://laravel.com/docs/4.2/pagination#appending-to-pagination-links

and

https://laravel.com/docs/4.2/requests

You need

whatever.site?page=2&id=5    //5 is just example here

In version 5.7 I have a where and it works fine:

    public function indexbl()
    {
        $dogs = DB::table('dc_dogs')->where('adopted', '=', 0)->paginate(5);
        $title = 'test';
        return view('dog.indexbl', compact('dogs', 'title'));
    }

view:

{{ $dogs->links() }}

This was a quick test, just starting to use blade.

munazzil's avatar

Try with these link method?

    {!! $data->links() !!}

Please or to participate in this conversation.