mongodb / laravel-mongodb

A MongoDB based Eloquent model and Query builder for Laravel (Moloquent)
https://www.mongodb.com/compatibility/mongodb-laravel-integration
MIT License
6.99k stars 1.42k forks source link

Sort by Relationship #1607

Closed kushan2 closed 4 years ago

kushan2 commented 5 years ago

How do we sort record by Relationship for example I have,

In my model,

class UserProfile extends Moloquent { protected $collection = 'user_profiles';

public function companies() {
    return $this->hasOne('App\Models\Company', '_id', 'company');
}

}

I want to sort UserProfile by Company name. Is it possible?

george-viaud commented 5 years ago

Perhaps sort companies by name, then iterate over the results, building a collection or array of users as you go using the inverse relationship users() in the Company model?

kawright129 commented 5 years ago

I have a situation in which I need to do this also. Has anyone found a resolution?