I would do this instead
$articles = Article::where('user_id', $id)->paginate(10);
Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.
i have hasMany relationship for user articles, This is my user model
public function articles() // returining correct data
{
return $this->hasMany(Article::class); // this get all aricles related to a user
}
in user controller im passing the data to the view like this
public function articles($id)
{
$subscriber = User::find($id)->with('articles');
return view('admin.subscribers.subscriberarticles', compact('subscriber'));
}
im trying to paginate the articles inside the user query above but its not working for me,
public function articles($id)
{
$subscriber = User::find($id)->with('articles')->paginate(10);
return view('admin.subscribers.subscriberarticles', compact('subscriber'));
}
is there is any other way to get this pagination done.
Sorry, missed the paginate part...that's a bit different. You call paginate() on the relationship method.
$subscriber = User::find($id)->articles()->paginate(10);
This would probably do the same thing, but more verbose...
$subscriber = User::with(['articles' => function($query) {
$query->paginate(10);
// $query->latest()->paginate(10); // order newest to oldest
}])->find($id);
Please or to participate in this conversation.