Home > Software design >  How to return all value of column after calculation laravel 8
How to return all value of column after calculation laravel 8

Time:10-10

I have a table that stores the bets from users. This bets table has relationship with users table.

id user_id bet_type bet_price bet_round
1 1 1 10 1
2 2 1 15 1

I select all from the bets table where bet_type = 1 and bet_round = $x_game_round . When I get them, I can calculate on bet_price column. In my case, I have two users with user_id=1 and user_id=2 who bet on bet_type=1. When I return, I can only see winning price for user_id=1. What I want is to return win_price for each user that bets with bet_type = 1.

I try in my controller as below.

$x_game_round = $x_value["gameRound"];
  

    if($x_value["winner"] === 1) {
        
        $winners = DB::table('bets')
        ->where('bet_type', '=', '1')
        ->where('bet_round', '=', $x_game_round)
        ->get();
       

        if($winners){
            foreach($winners as $winner) {
                $bet_price = $winner->bet_price;
                // echo $winner->user_id;
                $win_price = $this->calculateBasicPayout('1', $bet_price);
                return $win_price;

            }
        }
        
        
    }

public function calculateBasicPayout($bet_type, $bet_price){

        $commission = 5;
        
        if($bet_type === '1')
        {
            // take out 5% of Winning amount.
            $commissionAmount = ($commission / 100) * $bet_price;
            $payoutAmount = ($bet_price * 2) - $commissionAmount;
            return $payoutAmount;

        }elseif($bet_type === '2')
        {
            $payoutAmount = $bet_type * 2;
            return $payoutAmount;

        }elseif($bet_type === '3')
        {
            $payoutAmount = $bet_type * 8;
            return $payoutAmount;
        }
       
   }

CodePudding user response:

You’re only ever looping a single $winner as in your foreach loop you have a return statement which breaks out of your execution.

Instead you want to assign $winning_price to your $winner, or some other variable and remove the return statement.

  • Related