APPLE199's avatar

Find out which page has a specific record in pagination?

I'm working on a "forum like" app and when viewing a thread, a user can also choose to view a post in a thread (an specific one).

The URL can be something like /thread/29?post=22. Now, a thread can have many posts. Therefore, since it's paginated with 10 posts per page, a post can be in any page depending on when it was posted.

I have no way of knowing which page the post is on in the record.

My code:

$posts = Post::where('thread_id', $thread->id)->paginate(10);
//there are over 50+ posts for instance 

My simplified view:

@foreach ($posts as $post)

    <span id="post{{ $post->id }}">Post #{{ $post->id }}</span>

    {{ $post->body }}

@endforeach

My example URL would look like: http://example.com/thread/1

How would I know which page has a specific record? Say I want to go to the post with ID 28 which belongs to thread with an ID 1. How would I know which page of the result that thread is in?

I want to do something like:

http://example.com/thread/1?post=28&page=2 //authomatically guessed that post 28 would be in page 2.

how can do do this? Thanks!

0 likes
5 replies
jlrdw's avatar

Have a unique ID for each answer.

jlrdw's avatar

Probably no logical way of doing this short of finding that record only.

1 like
jekinney's avatar

As @jlrdw mentioned you'll have to set you your query, but not exacute it.

Paginate only returns the X amount of rows per page, so you can't find a specific answer if it's not on the paginated page. So set a base query, pluck your answer, then exacute your paginated query.

APPLE199's avatar
APPLE199
OP
Best Answer
Level 7

I have fixed it. Thank you for your support.

Please or to participate in this conversation.