I have a Model Topic
and Post
.
Each Topic
has many Post
.
My Topic
model extends Eloquent with a one-to-many relationship for Post
.
class Topic extends Eloquent
{
public function posts()
{
return $this->hasMany('Post');
}
}
My Post
model also extends Eloquent.
class Post extends Eloquent
{
public function topic()
{
return $this->belongsTo('Post');
}
}
In my TopicController
, I would like to paginate my posts through the relationship. I expended something like this would work:
$topic->posts->paginate(20);
However, it is a Collection
object. As such, I get the following error message.
Call to undefined method Illuminate\Database\Eloquent\Collection::paginate()
How do I correctly paginate a one-to-many relationship?