简体   繁体   中英

Laravel how to get current user rank by points?

I need to get current user rank by points. Example. I'm getting all total users count by:

$userCount = User::count();

I have users table with points column. USERS table:

USER ID | USERNAME | POINTS
1         USERA       32.25
2         USERB       2.25
3         USERC       -12.25

I need to take selected user rank by his points.

Updated: My blade to show user profile:

        public function show($slug)
    {
        /** @var User $user */
        $user = User::whereSlug($slug)->firstOrFail();
        if (! $user->isActivated()) {
            $this->alertInfo(trans('app.access_denied'));
            return;
        }
        $user->access_counter++;
        $user->save();
        $this->title("Player $user->username profile");
        $this->pageView('users::show', compact('user'));
    }

Route file:

ModuleRoute::context('Users');
    
ModuleRoute::resource('users', 'UsersController', ['only' => ['index', 'show', 'edit', 'update']]);
ModuleRoute::get('players/{slug}', 'UsersController@show')->name('users.show');

You have to use a simple SQL query, in this case Eloquent .

$user = User::max('points');

That should return the user with the highest point. Documentation

If you want to return the position for the currently authenticated User , you can do the following:

// get all users and order them by the `points` field in descending order
$usersByPoints = User::orderBy('points', 'desc')->get();

// search through your users to find the record where
// the name of the user record matches that of the autneticated user
$position = $usersByPoints->search(function ($user, $key) {
    return $user->name == auth()->user()->name;
});

// add 1 to the poisition as it will be zero indexed
return $position + 1;

update

Based on your comment, lets define a route in web.php :

Route::get('/user', UserProfileController::class);

Create the UserProfileController in your app/Http/Controllers directory using artisan (or manually if you prefer):

php artisan make:controller UserProfileController

Inside of this controller, define a single function called __invoke() which will be called by Laravel when someone accesses the /user route and add the code from above:

public function __invoke()
{
    $usersByPoints = User::orderBy('points', 'desc')->get();

    $position = $usersByPoints->search(function ($user, $key) {
        return $user->name == auth()->user()->name;
    });

    return view('user.profile', ['position' => $position + 1]);
}

In the above the return view() specifies the blade view to render when someone accesses the /user route and passes the position property to the view.

Create the blade view at resources/views/user/profile.blade.php . You can then access the $position property as follows:

Your position is {{ $position }}!

update 2 - doing it on the DB

MySQL does not have the concept of a row number function like other DB engines, however, the above query can be performed on the DB rather than in memory using PHP for those that care:

$position = (int) (DB::select(
    'select (@row_number:=@row_number + 1) as `position` from users, (select @row_number:=0) as t where id = ? order by points desc',
    [auth()->user()->id]
)[0])->position;

return $position + 1;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM