An user have many albums and albums has many photos, so:
I want to show a list of users order by latest updated photo.
$users = User::all();
// this will order photos but not the users...
$users->load('albums.photos', function($photos) {
$photos->orderBy('updated_at', 'DESC');
});
I can't seem to think of a way, using eloquent models and relationship as the relationships are loaded once the model is loaded.
Either we can use sql queries or use sort function in php to re-order the users.
It will be interesting if someone comes up with other answer using eloquent relationships.
Edit: Another thought
I want to show a list of users order by latest updated photo.
Use the inverse relation, fetch all photos order by updated at and load their users.
Well, I think that the inverse relation is not valid in my case, because could be the case that the first and the second photo belongs to the same user and I want to list the users (without repeating....)