webfuelcode's avatar

How to add the pagination on index

Using single thread list on index page with @include. How to use pagination on index as single page is working good.

Index page is here

@extends('layouts.front')

@section('heading')
    <a href="{{route('thread.create')}}" class="btn btn-success float-right">Create Thread</a><br>
@endsection

@section('content')

@include('layouts.partials.msg')

<h2>Threads</h2>

@include('thread.partials.thread-list')

@endsection

Single thread list is here thread.partials.thread-list

<div class="">
    @forelse ($threads as $thread)
        <div class="card my-2">
            <div class="card-body">
                <a href="{{route('thread.show', $thread->id)}}">
                    <h4 class="card-title">{{ $thread->subject }}</h4>
                </a>
                    <p class="card-text">{{ str_limit($thread->thread,100) }}</p>
                    <p class="card-link">Posted by: <a href="{{route('user_profile',$thread->user->name)}}">{{$thread->user->name}}</a> {{$thread->created_at->diffForHumans()}}</p>
                
            </div>
        </div>
        
    @empty
            <h5>No threads there</h5>
        
    @endforelse

    {{ $threads->links() }}
</div>
0 likes
8 replies
Snapey's avatar

What's the question? Sorry, don't get it.

webfuelcode's avatar

@snapey I have created a page for thread list and managed it to appear 5 posts at a time and display pagination below it. This single page works fine with pagination.

The same page I have included on the index page with @include

My question is, why the pagination does not appear there? or How to make a thread list with pagination on the index page?

I think @include display the whole pages pagination must appear there? I think.

Snapey's avatar

you have to send paginated eloquent collection to the index view

ApexLeo's avatar

@webfuelcode i assume u hvae a model name Thread.

did u paginate the Thread model like this.


$threads = Thread::paginate(); // default is 15

$threads = Thread::where( 'your condition' )->paginate(20) // you can assign rows per page;
webfuelcode's avatar

@snapey @neeraj1005

Here is the controller:

public function index()
    {
        $threads = Thread::orderBy('created_at', 'desc')->get();
        $threads = Thread::paginate(5);
        return view( 'thread.index', compact('threads') );
    }

@apexleo the thread model is here and I am not how to incert the paginate:

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Thread extends Model
{
    use CommentableTrait;
    protected $fillable=['subject', 'type', 'thread', 'user_id'];

    public function user()
    {
        return $this->belongsTo(User::class);
    }
}
SilenceBringer's avatar
Level 55

Hi @webfuelcode change your controller to

public function index()
    {
        $threads = Thread::orderBy('created_at', 'desc')->paginate(5);

        return view( 'thread.index', compact('threads') );
    }

also you can dump you data in thread.partials.thread-list

<div class="">
    {{ dd($threads, $threads->links()) }}
    @forelse ($threads as $thread)

to chek what you have on Home page

1 like
Neeraj1005's avatar

@webfuelcode change your code and you have to include paginate() in eloquent not nee to separately define.

Here is the video link regarding pagination https://www.youtube.com/watch?v=4yfRdl2-3mE

instead of this


$threads = Thread::orderBy('created_at', 'desc')->get();
 $threads = Thread::paginate(5);

use this

$threads = Thread::orderBy('created_at', 'desc')->paginate(5);

# ANd in blade do this

{{ $threads->links() }}
1 like

Please or to participate in this conversation.