jeudi 12 décembre 2019

Pagination for items in the Laravel relationship

I am very beginner in Laravel. I use in my project Laravel 5.8. 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();
    }
}

When I run this function:

ForumCategory::with('themes')->orderBy('name', 'asc')->paginate(35);

I have pagination only for pagination.

When I display the results of this function:

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

I can display pagination for $ forums.

I would also like to display the pagination for $ post (data from relationships). How can this make it?



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire