vendredi 13 décembre 2019

Links in pagination return error in Laravel 5.8

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

I want show list my posts with pagination.

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 <br/>
    @foreach ($forum->themes as $post)
      Post: 
<div class="paginationFormat"></div>
<div class="text-right"></div>
@endforeach
<div class="paginationFormat"></div>
<div class="text-right"></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?

I want show pagination for $forum and $post.

How can I do it?



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire