Support the ongoing development of Laravel.io →
Database Eloquent

Hi,

I have a pivot table that links users to products. The table has timestamps that are updated when I attach a product to a user.

When retrieving the products via something like User::products(). Is there a way to order on the date the relationship was created? ie, the created_at column in product_user

Cheers

James

Last updated 3 years ago.
0

Yes you can do something like this

User::with(array('products' => function($query) {
    $query->orderBy('created_at', 'desc');
}))->get();
Last updated 3 years ago.
0

Sign in to participate in this thread!

Eventy

Your banner here too?

weejames weejames Joined 18 Mar 2014

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2025 Laravel.io - All rights reserved.