nekooee's avatar

how to redirect back to the last page (when using pagination)?

Hi After the user submits a new post, he should return to the last page. Because a new page may have been added, or the user may not have posted from the last page.

Now I use the following code which goes back to the previous page, not the last page.

return back()->with(['flashMessage' => $message, 'flashSubject' => $subject]);
0 likes
12 replies
jlrdw's avatar

How is your orderBy, usually desc for post. Could you just redirect to page 1.

2 likes
nekooee's avatar

@jlrdw my order is ASC and the last post is on the last page.

If you do not have a solution for the last page, tell me how to return back to page 1?

nekooee's avatar

@jlrdw thank you

With your guidance, I wrote the code like this and it works fine:

        return redirect()->route('front.thread.show', [$thread->channel->slug, $thread->slug, 'page' => $thread->posts()->paginate(10)->lastPage()])->with(['flashMessage' => $message, 'flashSubject' => $subject]);
martinbean's avatar

@nekooee Why are your threads in chronological order? Surely you want threads in reverse chronological order so that the most recent thread at the top of the first page, and not the last item on page 452 or whatever.

2 likes
jlrdw's avatar

@martinbean I was thinking same, to use desc. But there are cases where you goto last, like a ledger entry in accounting.

1 like
nekooee's avatar

@martinbean In many forums, including the laracasts Forum, the latest posts are on the last pages.

martinbean's avatar

@nekooee No, they’re not. The most recently-replied to thread is at the top of the first page.

2 likes
nekooee's avatar

@martinbean What I see now are posts in ASC order. And the replies to each post are also in ASC order below itself.

screen shot

nekooee's avatar

@jlrdw I do not have replies in my forum. There are threads and posts. The title and body of the thread are always seen at the top of all pages. And the posts are in the order below. The same is true for vBulletin forums. But the user can change the order and personalize.

Snapey's avatar

only you know what 'last' means in your context

Query the database to find the last record then redirect there

2 likes

Please or to participate in this conversation.