nikocraft's avatar

how to call links() with after ajax update?

Is it possible to regenerate pagnation after I have made ajax call that changes number of pages that would be returned?

this is how I call it in the blade

$comments->links()

but after I made ajax call I would like to regenerate pagination without having to reload the page, has anyone done it and how?

0 likes
6 replies
jekinney's avatar

As @jlrdw said.

You can not regenerate php on the client side. You can manipulate the rendered (HTML) output but there is no guarantee that it'll be the same so that causes bugs and a whole lot of checks in JavaScript.

Much simpler to use the data from the ajax object to set up links.

1 like
nikocraft's avatar

@jlrdw

great guys thanks, this is what I did but have run into a simple problem, please read:

public function status(Request $request)
{
    if($request->ajax()) {
        $comment = Comment::where('id', $request->comment_id)->first();
        $comments = Comment::latest()->paginate(3);
        $comment->status = $request->status;
        $comment->save();
        return response()->json([
            'status' => 'success',
            'comment' => $comment,
            'pagination' => view('core.comments.pagination', compact('comments'))->render(),
        ], 201);
    }
}

my routes:

        Route::get('/', ['as' => 'backend.comments.index', 'uses' => 'CommentController@index']);
        Route::post('status', ['as' => 'backend.comments.status', 'uses' => 'CommentController@status']);

I have a problem now as you can see function status is called, and the problem is that pagination generated applys status in the url so the link ends up being

http://larapress.dev/backend/comments/status?page=2

instead of

http://larapress.dev/backend/comments?page=2

second one works and is generated corrctly when I first enter the page, but as soon as i update the status on the model and new pagination is sent back it has added status in the url and that fails as you can understand. Is there a way around this?

Please or to participate in this conversation.