skip to Main Content

I have two tables bets and users.

bets columns

  1. tickets (json)
  2. draw_date
  3. user_id

I would like to get all tickets from users of a specific draw_date.

Example result:

[
 1 => [283983, 389239, 484843],
 2 => [122343, 332344]
]

1, 2 will be the user ids. Tickets array will contain tickets from all bets of a individual user.

2

Answers


  1. SELECT user_id, JSON_ARRAYAGG(ticket) AS tickets
    FROM bets
    WHERE draw_date = 'specific_draw_date'
    GROUP BY user_id;
    
    Login or Signup to reply.
  2. use IlluminateSupportFacadesDB;
    use AppModelsBet; // Assuming your model for the 'bets' table is Bet
    
    $specificDrawDate = 'your_specific_draw_date'; // Replace with the actual date
    
    $results = Bet::select('user_id', DB::raw('JSON_ARRAYAGG(tickets) as tickets'))->where('draw_date', $specificDrawDate)->groupBy('user_id')->get()->mapWithKeys(function ($item) {
    // Assuming tickets are stored as JSON arrays in the database
       return [$item->user_id => json_decode($item->tickets)];
    });
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search