tradaraka
tradaraka

Reputation: 53

Links in pagination return error in Laravel 5.8

I'm a beginner in Laravel. I use Laravel 5.8 in my project.

I want to show list my posts with pagination.

I have a 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 a pagination in $post.

In my code I have error:

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

How can I repair it?

I want to show pagination for $forum and $post.

How can I do it?

Upvotes: 0

Views: 107

Answers (1)

Sohail Ahmed
Sohail Ahmed

Reputation: 1496

First of all, you should use a controller to return paginated data. But if you want to achieve the results in the same code, Do something like this.

@foreach($forum->themes()->paginate(10) as $post)

Upvotes: 1

Related Questions