mardi 25 juillet 2017

Laravel Eloquent - Order users relationship by a method on the user model

I'm working on an API endpoint with Laravel Spark.

This endpoint returns the given Team along with its users.

// in App\Team

public function users()
{
   return $this->belongsToMany(
       'App\User', 'team_users', 'team_id', 'user_id'
   )->withPivot('role');
}

However, I wish to order those users by a method that is on the user model.

On my App\User model I have a method:

public function currentQueueLength()
{
    return an integer
}

Is there any way I can return the users relationship but order the users by the result of that method?



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire