I want to paginate all my articles. 15 of them per page.
Here is the call to paginate:
Article::orderBy('created_at', 'desc')->paginate(15)
The pagination works fine but the problem is that on each article Laravel makes a query on my users table. 15 articles per page, 15 duplicate queries to my users table. Here is the image from debugbar: image source
How can I optimize the pagination and erase all those duplicate queries?
public function articles() { return $this->hasMany(Article::class); }and in articles I havepublic function user() { return $this->belongsTo(User::class); }. - ealocin