1

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

MEDZ
  • 2,227
  • 2
  • 14
  • 18
tradaraka
  • 53
  • 6

2 Answers2

0

Maybe you could take a look at the Pagination documentation and examples from the Laravel docs: https://laravel.com/docs/5.8/pagination

The Laravel docs are a great place to start since everything is documented very well and there are a lot of examples.

If you want to add a page that paginates all posts, just return a view with Post::sortBy('name')->paginate() and use $posts->links().

Tim
  • 551
  • 3
  • 23
0

If you have to do pagination an Eager loading constraint - it won't work, else you would have to use the constraint to execute the query as the parent Model/Object.

Refer to this link, go through it. https://laracasts.com/discuss/channels/laravel/paginate-constrained-eager-load

Use Themes as the Parent Object and paginate accordingly depending on the relationship defined in the models.

Themes::with(['forumCategory'=>function(query){
   //condition here.... 
}])-paginate(10); 

Or alternatively you have multiple paginations in one view but passed separately:

 $themes =  Themes::with('forumCategory')-paginate(10); 
  $forums =  ForumCategory::with('themes')->orderBy('name', 'asc')->paginate(35);

Same issue in this link: Laravel Multiple Pagination in one page

And you might have to write an anonymous helper function to handle relative actions that involve both Models somewhere in your application.

Alternatively you could use lazyEager loading on the post model. So while you have this in your code, you could do lazy eager loading on the themes.

$forums = ForumCategory::with('themes')->orderBy('name', 'asc')->paginate(35);
$forums->load(['themes' => function($query) {
       $query->take(5);
}]);

you could also utilize the loadCount() function.

halfer
  • 19,824
  • 17
  • 99
  • 186
Ande Caleb
  • 1,163
  • 1
  • 14
  • 35