I have a plugin with 2 components. One is for 'posts', and the other is for a 'profile'. The profile belongs to a user and hasMany 'posts'.
However when I access the relationship it loads every post. I just want to load 5 then paginate or lazy load, how can I do this?
Profile.php model
public $hasMany = [
'posts' => [
'Redstone\Membership\Models\Post',
'table' => 'redstone_membership_profiles_posts',
]
];
public $belongsTo = [
'user' => [
'Rainlab\User\Models\User',
]
];
Post.php model
public $belongsTo = [
'profile' => ['Redstone\Membership\Models\Profile',
'table' => 'redstone_membership_profiles_posts',
]
];
Profile.php component
protected function loadProfile()
{
$id = $this->property('profileUsername');
$profile = new UserProfile
$profile = $profile->where(['slug' => $id]);
$profile = $profile->first();
return $profile;
}
profile/default.htm - component view
{% set profile = __SELF__.profile %}
{% set posts = __SELF__.profile.posts %}
{% for post in posts %}
<div class="card">
<div class="card-header">
<div class="ml-2">
<div class="h5 m-0">{{ profile.title }}</div>
</div>
{{ post.published_at|date('M d') }}
</div>
<div class="card-body text-left">
{{ post.content|raw }}
</div>
</div>
{% else %}
<h1>This user has not made any posts.</h1>
{% endfor %}