Initialer Laravel Commit für BetiX
This commit is contained in:
35
app/Http/Controllers/RecentlyPlayedController.php
Normal file
35
app/Http/Controllers/RecentlyPlayedController.php
Normal file
@@ -0,0 +1,35 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers;
|
||||
|
||||
use App\Models\GameBet;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Facades\Auth;
|
||||
|
||||
class RecentlyPlayedController extends Controller
|
||||
{
|
||||
/**
|
||||
* GET /api/recently-played
|
||||
* Returns up to 8 distinct recently played games for the authenticated user.
|
||||
*/
|
||||
public function index(Request $request)
|
||||
{
|
||||
$user = Auth::user();
|
||||
abort_unless($user, 401);
|
||||
|
||||
// Use a subquery to get the latest bet per game_name
|
||||
$games = GameBet::where('user_id', $user->id)
|
||||
->select('game_name', \Illuminate\Support\Facades\DB::raw('MAX(created_at) as last_played_at'))
|
||||
->groupBy('game_name')
|
||||
->orderByDesc('last_played_at')
|
||||
->limit(8)
|
||||
->get()
|
||||
->map(fn($row) => [
|
||||
'game_name' => $row->game_name,
|
||||
'slug' => strtolower(preg_replace('/[^a-z0-9]+/i', '-', $row->game_name)),
|
||||
'last_played_at' => $row->last_played_at,
|
||||
]);
|
||||
|
||||
return response()->json(['data' => $games]);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user