Posting with Paginated Data

Hi Guys,

I’m building a mini forum and as I post on a paginated thread and refresh the page via livewire, how can I check the current page?

The problem is if I post, exceeding the post per page limit, I need to redirect to page 2 after posting a post, so the user can view there new post, if that makes sense ?

Hey, @AtomCoder

You can use something like that:

 $model->appends(Request::except('page'))->render()

Hi @skywalker

How could I use that exactly?

Just show me the code, and I’ll give the right answer

My Render method:

public function render()
{
  $thread = ForumThreads::where('id', $this->threadID)->with(['posts'])->first();
  $posts = $thread->posts()->paginate($this->paginationCount);

  return view('livewire.forum.thread')->with(compact('thread', 'posts'));
}

My Post Reply method:

public function postReply()
{
  $this->validate();

  # Post Reply
  $test = ForumPosts::create([
    'userID' => Auth::user()->id,
    'threadID' => $this->threadID,
    'description' => $this->reply,
  ]);

  # Update The Component 
  $this->emit('refreshPosts');

  $this->reply = '';

  # Display Toast
  $this->dispatchBrowserEvent('toast', [
    'message' => 'Reply posted.',
    'type' => 'success',
  ]);
}

You can take a look here

$paginator->currentPage()

https://laravel.com/docs/8.x/pagination#paginator-instance-methods

Yes I’m aware of those available methods, but my issue is that I need to figure out what page is next and if its a new page specifically, so that I can redirect from my controller method after the post is submitted.

@skywalker

Ok I’m able to access $paginator->currentPage() in the controller. Again, my problem is if the user creates a new post and as a result of the new post being created it creates a new page, how can I refresh the posts on the new page?

Currently it posts and refreshes as expected but when a new page is created I manually need to navigate to the last page to see the content.