Dronax
492
32
Laravel

Get position in rating by scope

Posted 6 months ago by Dronax

Hello. In model I have this scope:

public function scopeRate($query)
{
    return $query->orderByDesc('points')->latest();
}

I need get position item in rating by points. How I can do it?

Example:

#1 Item - 900 points #2 Item - 800 points #3 Item - 50 points #4 item - 0 points

How I can get numbers 1,2,3,4 ?

I tryied this:

public function getPositionAttribute()
{
    return static::query()->where($this->getKeyName(), '<=', $this->getKey())->count() ;
}

But thisi is not working. I get various numbers.

Please sign in or create an account to participate in this conversation.