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

FREDERIC LD's avatar

Sorting an eloquent collection using pivot table date field

Hello,

I need to sort a collection of models based on a pivot table date

I have 2 models 'users' and 'Content'

when an activity has been completed by a user it is marked as 'completed' with a date in a pivot table

I am trying to select all the content viewed by a user sorted by date

Model content

    public function activityUsers()
    {
        return $this->belongsToMany(User::class, 'content_activity_user')->withTimestamps();
    }
Content::select('id', 'summary_heading', 'summary_text', 'slug')
                            ->where('template_id', 3)
							->with('activityUsers', function($query) use ($userId){
                                $query->select('content_activity_user.updated_at');
                                $query->where('user_id', $userId);
                            })
                            ->whereHas('activityUsers', function($query) use ($userId){
                                $query->where('completed', 'Y');
                                $query->where('user_id', $userId);
                            })
                            ->with(['media' => function (MorphMany $query) {
                                $query->where('collection_name', 'banner');
                            }])
                            ->orderBy('**********, 'desc')   // needs to sort by content_activity_user.updated_at
                            ->limit(4)
                            ->get();

The above query returns all the activities tagged as completed For each activity in the collection, I have the activityUsers pivot table with the 'updated_at' date

I just can't figure out how to sort the result by content_activity_user.updated_at

0 likes
1 reply
MichalOravec's avatar
Level 75

You need to use join for that

->join('content_activity_user', 'contents.id', '=', 'content_activity_user.content_id')
->orderByDesc('content_activity_user.updated_at')

Please or to participate in this conversation.