I'm trying to obtain a ranking table based on how much points that has an User.
My User model is (simplified):
namespace Gamify;
class User extends Model
{
protected $table = 'users';
public function points()
{
return $this->hasMany('Gamify\Point');
}
public function scopeMember($query)
{
return $query->where('role', '=', 'default');
}
}
And Point model is:
namespace Gamify;
class Point extends Model
{
protected $table = 'points';
protected $fillable = array(
'points',
'description'
);
public function user()
{
return $this->belongsTo('Gamify\User');
}
}
I'd like to obtain a Collection of users with the sum of its points, ordered by this sum.
Something like this (this code is only a mockup):
public static function getRanking($limitTopUsers = 10)
{
return User::member()->orderBy(sum('points'))->get();
}
I've been playing with User::with() and scopes and I'm trying not to use DB::raw().
Can anyone help me? Thanks in advance.
via Chebli Mohamed
Aucun commentaire:
Enregistrer un commentaire