[英]Laravel how to get current user position by points
我想知道如何通過用戶積分獲得當前用戶位置?
例子:
User1 = 點數:100 User2 - 點數:150 User3 - 點數:360
所以用戶 3 將是第一個,用戶 2 第二個等等。
我通過以下方式獲取所有用戶:
{{ User::count() }}
用戶表有一個點行。
任何例子或幫助? 謝謝你們!
您可以使用 laravel 的查詢生成器,如下所示。
DB::table('users')->orderBy('points', 'desc')->get()
您可以按用戶的積分排序。 例如。 User::query()->orderBy('points','DESC')->get();
編輯:
如果用戶表不是那么大,您可以嘗試使用行號獲取所有用戶,然后按點搜索集合。
DB::statement(DB::raw('set @row:=0')); $users = User::selectRaw('*, @row:=@row+1 as position')->orderBy('points','DESC')->get();
$積分 = 150; $users_by_points = $users->where('points',$points)->values();
如果您有相同點數的用戶,您可以嘗試按點數 + 行號進行分組。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.