trifek's avatar

Links() not working in Laravel 5.8

Hi, I'a beginner in Laravel. I use Laravel 5.8 in my project.

I have problem with pagination.

I have this code:

class ForumCategory extends Model
{
    use scopeActiveTrait;

    protected $quarded = ['id'];
    protected $fillable = ['company_id', 'enable', 'name', 'url_address', 'enable'];
    public $timestamps = false;

    public function themes()
    {
        return $this->hasMany('App\ForumPost', 'id_category', 'id')->where('parent_id', '=', null);
    }

    public function lastPost()
    {
        return $this->themes()->orderBy('id', 'desc')->first();
    }
}

class ForumPost extends Model
{
    use scopeActiveTrait;

    protected $quarded = ['id'];
    protected $fillable = ['company_id', 'id_category', 'parent_id', 'user_id', 'date', 'title', 'content', 'url_address', 'enable'];
    public $timestamps = true;
    protected $table = 'forum';

    public function category()
    {
        return $this->belongsTo('App\ForumCategory', 'id_category');
    }

    public function author()
    {
        return $this->belongsTo('App\User', 'user_id');
    }

    public function postCount()
    {
        return $this->hasMany('App\ForumPost', 'parent_id', 'id')->where('parent_id', '!=', null)->count();
    }
}



@foreach ($forums as $forum)
Welcome in {{ $forum->name }}<br/>
    @foreach ($forum->themes as $post)
      Post: {{ $post->title }}
<div class="paginationFormat"></div>
<div class="text-right">{{ $post->links() }}</div>
@endforeach
<div class="paginationFormat"></div>
<div class="text-right">{{ $forums->links() }}</div>
@endforeach

I run this code by function:

public function getForumCategories()
    {
        $forumCategories = ForumCategory::with('themes')->orderBy('name', 'asc')->paginate(1);
        if(!$forumCategories->isEmpty()) {
            foreach ($forumCategories as $category) {
                $category->setRelation('themes', $category->themes()->paginate(1));
            }
        }
        return $forumCategories;
    }

I need pagination in $post.

In my code I have error:

Call to undefined method App\ForumPost::links() (View: ....)

How can I repair it?

0 likes
1 reply
Sinnbeck's avatar
Sinnbeck
Best Answer
Level 102

You are binding the paginate to $themes not $post

Like this

<div class="text-right">{{ $forum->themes->links() }}</div>

Please or to participate in this conversation.