Trying to think of alternate ways to get what I need from my API.
I am using Laravel Spark and have a query along the lines of:
$team = Team::with('users')->find($id);
The users relationship on the Team model is:
public function users()
{
return $this->belongsToMany(
'App\User', 'team_users', 'team_id', 'user_id'
)->withPivot('role')->orderBy('current_active_team', 'DESC');
}
I am currently ordering this by a field on the users table, but I also want to order by a method on the Users model:
public function queueLength()
{
// returns an integer for the users queue length
}
To return this data I am currently adding this under my query:
foreach ($team->users as $user) {
$user->queueLength = $user->queueLength();
}
Is there a way I can order the $team->users by their queueLength?