Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

mstnorris's avatar

@JarekTkaczyk from seeding my database with random threads and posts.

A Thread (that has been answered) takes me to page 1 but the answer is actually on page 3... Ok, so it looks like it is always taking me to page 1.

public function showAnswer($thread_id, $answer_id)
    {
        $perPage = 12;
        $post = Post::find($answer_id);
        $thread = Thread::find($thread_id);
        $page = $thread->posts() //
            ->latest()
            ->selectRaw('ceil(count(*) / ?) as page', [$perPage])
            ->where('created_at', '>=', $post->created_at)
            ->pluck('page');

        $anchor = $post->id; // get the html anchor of the answer, id or whatever you use

    return redirect("/discussion/$thread_id/?page={$page}#{$anchor}");
    // the url might be something like "/threads/{$post->thread_id}?page..."
    }

If I change the ordering to be oldest() (as that is how I show my posts) then I get random pages.

JarekTkaczyk's avatar

@mstnorris You must make it consistent. If you sort your replies by oldest, then in this method also use oldest.

mstnorris's avatar

@JarekTkaczyk I have just updated my response. I was using oldest() and that is what gives me random results. It doesn't show the correct page.

JarekTkaczyk's avatar

@mstnorris Well, I'm gonna start charging you for this consulting :)

My guess is that you have test data in your posts table. While it is unlikely to have a page (or more) of posts created in the very same second (equal timestamp in the table), it's common with test data, right?

Having said that, you need to rely on something else, because non-unique timestamp field is not reliable (probably OK in real life, but let's handle this):

posts table
 id |     created_at
  1 | 2015-03-31 12:00:00
  2 | 2015-03-31 12:00:00
  3 | 2015-03-31 12:00:00
  4 | 2015-03-31 12:00:00
  5 | 2015-03-31 12:00:00
  6 | 2015-03-31 12:00:00

post 1 is on page 1, but the query runs created_at <= 2015-03-31 12:00:00, so it returns page 3.

In order to make it reliable you need eg. id column as well:

      $page = $thread->posts()
            ->selectRaw('ceil(count(*) / ?) as page', [$perPage])
            ->where('created_at', '<=', $post->created_at) // if oldest, then <= operator
            ->where('id', '<=', $post->id)
            ->pluck('page');

then in the controller showing thread with posts just use ->oldest()->orderBy('id', 'asc') and the page will be just fine.

mstnorris's avatar

@JarekTkaczyk

I know, thank you for your help. I don't know how you do it. Not only debugging someone else's code, but code that you can't see! Thank you.

I don't think adding the where clause for $post->id will help as they are not in sequential order.


This is what a link on my index page looks like

http://myapp.dev/discussion/161/3311

The is the corresponding route

Route::get('discussion/{thread_id}/{post_id}', ['as' => 'show_answer_path', 'uses' => 'DiscussionsController@showAnswer',  'middleware' => 'auth']);

Here is the DiscussionsController@showAnswer method

public function showAnswer($thread_id, $answer_id)
    {
        $perPage = 12;
        $post = Post::find($answer_id);
        $thread = Thread::find($thread_id);
        $page = $thread->posts()
            ->oldest()
            ->selectRaw('ceil(count(*) / ?) as page', [$perPage])
            ->where('created_at', '>=', $post->created_at)
            ->where('id', '<=', $post->id)
            ->pluck('page');

        $anchor = $post->id; // get the html anchor of the answer, id or whatever you use

    return redirect("/discussion/$thread_id?page={$page}#{$anchor}");
    // the url might be something like "/threads/{$post->thread_id}?page..."
    }

and here is the generated url - http://myapp.dev/discussion/161?page=1#3311

this should be on page 2 but it takes me to page 1

Here is that row from my threads table (there are 37 replies - see below)

Here are the associated posts (I am randomly assigning date and times too so collisions are very unlikely)

What else should I show you? It always takes me to page 1.

Previous

Please or to participate in this conversation.