Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Corbin's avatar

How do I paginate my activity feed relationship in a JSON Resource API?

I'm building a JSON API profile activity feed that includes both posts and comments. What I'm trying to do is paginate the latest activity.

User Model

    public function activity()
    {
        return $this->hasMany('App\Activity');
    }
    
    public function latestActivity()
    {
        return $this->activity()->latest()->paginate(1);
    }

ProfileController

    public function show(User $user)
    {
        $user->load(['activity']);
    
        return new UserResource($user);
    }

I've tried both $user->load(['activity']); and $user->load(['latestActivity']); Both different errors:

1.) $user->load(['latestActivity']);

Call to undefined relationship [latestActivity] on model [App\User].

    public function activity()
    {
        return $this->hasMany('App\Activity')->latest()->paginate(1);
    }

2.) $user->load(['activity']);

I changed the activity relationship in the user model to be: return $this->activity()->latest()->paginate(1); and the UserResource to 'activity' => new ActivityCollection($this->whenLoaded('activity')),

Method Illuminate\Database\Eloquent\Collection::addEagerConstraints does not exist.

I eager load the subjects of my activity in the Activity model:

Activity Model

protected $with = ['subject'];

UserResource

    public function toArray($request)
    {
        return [
            'name' => $this->name,
            'username' => $this->username,
            'slug' => $this->slug,
            'bio' => $this->bio,
            'activity' => new ActivityCollection($this->whenLoaded('activity')),  
        ];
    }

This works, but it doesn't show the pagination in the JSON output:

class UserResource extends JsonResource
{
    /**
     * Transform the resource into an array.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return array
     */
    public function toArray($request)
    {
        return [
            'name' => $this->name,
            'username' => $this->username,
            'slug' => $this->slug,
            'bio' => $this->bio,
            'activity' => new ActivityCollection($this->activity()->latest()->paginate(2)),  
        ];
    }
}

I'll probably have to create another Resource called profile resource for that though, since that would lead to an n+1 when the UserCollection is called.

How do I paginate my activity feed in a JSON API?

0 likes
2 replies
mohansharma's avatar

You can't load latestActivity method because it does not return a relationship instance like activity method does.

1 like
kevinbui's avatar

What about updating your relationships like this:

class User extends Model
{
    public function activity()
    {
        return $this->hasMany('App\Activity');
    }
    
    public function latestActivity()
    {
        return $this->hasOne('App\Activity')->latest();
    }
}

So you can use both activity and latestActivity.

Please or to participate in this conversation.