Play Free
Play Free

Reputation: 105

Why do I get the same value when using paginate in Laravel?

I just encountered this problem recently, I am using Laravel's pagination I also use ajax. If I have 2 dates that are the same, for example, I posted something today and then posted again today, when I go to the next page it just shows me the same values as the last inserted post.

Controller:

public function table() {
    $data= table::where('date_when','>=',today())->orderBy('date_when','ASC')->paginate(2);
    return view('pages.table1',compact('data'));
}
public function fetch_data(Request $request) {
    if($request->ajax()) {
        $PIData = table::where('date_when','>=',today())->orderBy('date_when','ASC')->paginate(2);
        return view('tables.table',compact('PIData'))->render();
    }
}

and then I use on my view $data->links()

Script

function paginateTable() {
    $(document).on('click', '.pagination a', function(event) {
        event.preventDefault();
        var page = $(this).attr('href').split('page=')[1];
        fetch_data(page);
    });

    function fetch_data(page) {
        $.ajax({
            url:"/fetch_data?page="+page,
            success:function(data) {
                $('#table_data').html(data)
            }
        });
    }
}

I use two views, one for displaying the table for me to use the fetch_data on the controller, and one for including the view which displays the table.

Upvotes: 1

Views: 310

Answers (1)

LoveCoding
LoveCoding

Reputation: 1211

Please try to do this.

$(document).on('click', '.pagination a', function(event) {
        event.preventDefault();
        var page = $(this).attr('href');
        fetch_data(page);
    });

    function fetch_data(page=null) {
        $.ajax({
            url:page?? "/fetch_data",
            success:function(data) {
                $('#table_data').html(data)
            }
        });
    }

Upvotes: 1

Related Questions