dtommy79's avatar

Pagination isn't working

Hello!

I have a simple forum. On each topics the first post is the "topic" or the question, and under it the "replies" are listed. I'd like to paginate the replies, but it doesn't seem to work for me since all the replies are displayed on the page and they are not getting paginated. The paginator is displayed though.

This is my controller, the query that should control the pagination is the $repliesToPaginate

public function showSingleTopic($id, $topicSlug)
    {
        $forumCategories = DB::table('forum_categories')->orderBy('title', 'ASC')->get(); 

        $topic = Topic::find($id);

        if($topic) {
            $topic->increment('views', 1);
        }


        $currentTopic = Topic::with('replies')->where('id', $id)->first();

        $repliesToPaginate = Reply::with('topic')->where('topic_id', $currentTopic->id)->paginate(2)->withQueryString();

        return view('frontend.community.topic-single', compact('forumCategories', 'topic', 'repliesToPaginate'));
    }

In my Topic model I have a hasMany relationship with the replies like this:

public function replies()
    {
        return $this->hasMany(Reply::class);
    }

In my Reply mode I have a bleongsto

public function topic()
    {
        return $this->belongsTo(Topic::class);
    }

Here is my table details for topics:

$table->bigIncrements('id');
            $table->string('title', 191)->unique();
            $table->string('slug', 191);
            $table->string('meta_title')->nullable();
            $table->string('meta_description')->nullable();
            $table->text('content');
            $table->integer('views')->default(0);
            $table->unsignedBigInteger('forum_category_id');
            $table
                ->foreign('forum_category_id')
                ->references('id')
                ->on('forum_categories')
                ->onUpdate('CASCADE')
                ->onDelete('CASCADE');
            $table->unsignedBigInteger('user_id');
            $table
                ->foreign('user_id')
                ->references('id')
                ->on('users')
                ->onUpdate('CASCADE')
                ->onDelete('CASCADE');

            $table->index('title');
            $table->timestamps();

And for the replies

$table->bigIncrements('id');
            $table->text('content');
            $table->unsignedBigInteger('topic_id');
            $table
                ->foreign('topic_id')
                ->references('id')
                ->on('topics')
                ->onUpdate('CASCADE')
                ->onDelete('CASCADE');
            $table->unsignedBigInteger('user_id');
            $table
                ->foreign('user_id')
                ->references('id')
                ->on('users')
                ->onUpdate('CASCADE')
                ->onDelete('CASCADE');
            $table->timestamps();

This is my view

                <!-- Original post (first post of the topic) -->
                <div class="single_topic_container mb-2" style="border: 6px solid #e65b2e38; ">

                    <div class="single_topic_main_content">
                        <div class="single_topic_user_data">
                            <div class="user_image">
                                <img src="{{ asset('public/uploads/images/user/' . $topic->user->profile_image) }}" class="img-fluid" alt="{{ $topic->user->username }}">
                            </div>
                            <div class="user_meta text-center">
                                <p class="username">{{ $topic->user->username }}</p>
                                @if($topic->user->role_id == 1)
                                <p class="role">Admin</p>
                                @else
                                <p class="role">Member</p>
                                @endif
                                <p class="post">Topics:</p>
                                <p class="post_counter">{{ count($topic->user->topics) }}</p>
                            </div>
                        </div>
                        <div class="single_topic_content">
                            <h1>{{ $topic->title }}</h1>
                            <div class="single_topic_body mt-1">
                            {!! \Purify::clean($topic->content) !!}
                            </div>
                        </div>
                    </div>
                    <div class="single_topic_footer">
                        <div>
                            <p><i class="bi bi-clock"></i> Posted on: {{ $topic->created_at->diffForHumans() }}</p>
                        </div class="created">
                        <div class="actions flex">
                            @if(Auth::check())
                                @if( Auth::user()->id == $topic->user->id || Auth::user()->role_id == 1)
                                    <div class="edit">
                                        <a href="{{ route('member.topic.edit', $topic->id) }}" title="Edit reply"><i class="bi bi-pencil-square"></i></a>
                                    </div>
                                @endif
                                @if(Auth::user()->role_id == 1 )
                                    <div class="delete ml-1">
                                        <form action="{{ route('member.topic.delete', $topic->id) }}" method="POST">
                                            @method('DELETE')
                                            @csrf
                                        <button onclick="return confirm('Are you sure you want to delete this item?');" type="submit" title="Delete reply" class="action_delete radius-3"><i class="bi bi-trash-fill"></i></button>
                                        </form>
                                    </div>
                                @endif
                            @endif
                        </div>
                    </div>
                </div>

               


                <!-- Replies here -->
                @if(count($topic->replies) > 0)
                    @foreach($topic->replies as $reply)
                    <div class="single_topic_container mb-1">

                        <div class="single_topic_main_content">
                            <div class="single_topic_user_data">
                                <div class="user_image">
                                    <img src="{{ asset('public/uploads/images/user/default-user-avatar.png') }}" class="img-fluid" alt="">
                                </div>
                                <div class="user_meta text-center">
                                    <p class="username">{{ $reply->user->username }}</p>
                                    @if($reply->user->role_id == 1)
                                    <p class="role">Admin</p>
                                    @else
                                    <p class="role">Member</p>
                                    @endif
                                    <p class="post">Replies:</p>
                                    <p class="post_counter">{{ count($reply->user->replies) }}</p>
                                </div>
                            </div>
                            <div class="single_topic_content">
                                <div class="single_topic_body">
                                {!! \Purify::clean($reply->content) !!}
                                </div>
                            </div>
                        </div>
                        <div class="single_topic_footer">
                            <div>
                                <p><i class="bi bi-clock"></i> Posted on: {{ $reply->created_at->diffForHumans() }}</p>
                            </div class="created">
                            <div class="actions flex">
                                @if(Auth::check())
                                    @if( Auth::user()->id == $reply->user->id || Auth::user()->role_id == 1)
                                    <div class="edit">
                                        <a href="{{ route('member.topic.reply.edit', $reply->id) }}" title="Edit reply"><i class="bi bi-pencil-square"></i></a>
                                    </div>
                                    @endif
                                    @if(Auth::user()->role_id == 1 )
                                    <div class="delete ml-1">
                                        <form action="{{ route('member.topic.reply.delete', $reply->id) }}" method="POST">
                                            @method('DELETE')
                                            @csrf
                                        <button onclick="return confirm('Are you sure you want to delete this item?');" type="submit" title="Delete reply" class="action_delete radius-3"><i class="bi bi-trash-fill"></i></button>
                                        </form>
                                    </div>
                                    
                                    @endif
                                @endif

                                
                            </div>
                        </div>

                    </div>
                    @endforeach
                    <!-- Pagination -->
                    <div>
                        {{ $repliesToPaginate->links('layouts.partials.paginator') }}
                    </div>
                    <!-- Pagination ends-->
                   
                @else 
                <h2 class="text-center mt-2 mb-2">There are no replies to this post yet! Be the first to answer!</h2>
                @endif


                <!-- Post reply form -->
                @if(! Auth::check())
                    <h3 class="text-center"><a href="{{ route('login') }}">Please log in to post a reply!</a></h3>
                @else 
                <div class="mb-1 mt-2">
                    <h3>Post a reply!</h3>
                </div>
                <div class="reply_form_container mb-1">

                    <div class="reply_main_content">
                        <form action="{{ route('member.topic.reply.save', $topic->id) }}" method="post">
                            @csrf
                            <div class="reply_top">
                                
                                <div class="reply_form">
                                    <textarea name="content" rows="10" id="content" cols="30" placeholder="Type your reply here ....">{{old('content')}}</textarea>
                                    <p style="color:red;">{{ $errors->first('content') }}</p>
                                </div>
                            </div>
                            
                        
                            <div class="single_topic_footer">
                                <button type="submit" class="btn btn-m btn-default">Submit reply</button>
                            </div>
                        </form>
                    </div>
                </div>
                @endif



Any help is appreciated

0 likes
5 replies
Sinnbeck's avatar

Show how you are displaying them in blade.

Btw. You can remove this line as it does nothing

$currentTopic = Topic::with('replies')->where('id', $id)->first();//remove this 

//and do
$repliesToPaginate = Reply::where('topic_id', $id)->latest()->orderBy('id')->paginate(2)->withQueryString();

Be aware that I fixed your query a bit as well

dtommy79's avatar

@Sinnbeck Thanks. I updated my original post with my view. I tried the new query, but didn't work

Sinnbeck's avatar
Sinnbeck
Best Answer
Level 102

@dtommy79 ok. This part is wrong

@foreach($topic->replies as $reply)

Should be

@foreach($repliesToPaginate as $reply)
1 like
Sinnbeck's avatar

@dtommy79 happy to help. Hope you see why (it's using the variable you added pagination to)

Please or to participate in this conversation.