Advertisement
Guest User

Untitled

a guest
Jun 20th, 2019
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.13 KB | None | 0 0
  1.  public function showUserTransactionMonthly($userId)
  2.     {
  3.  
  4.         $monthlyTransaction = [];
  5.         for ($i = 1; $i <= 12; $i++){
  6.             $monthIsFraud  = Transaction::whereMonth('created_at',$this->generateMonth($i))
  7.                 ->where('is_fraud',1)
  8.                 ->where('user_id',$userId)
  9.                 ->count();
  10.             $monthIsNotFraud  = Transaction::whereMonth('created_at',$this->generateMonth($i))
  11.                 ->where('is_fraud',0)
  12.                 ->where('user_id',$userId)
  13.                 ->count();
  14.  
  15.             $monthlyTransaction[] = [
  16.                 'valueIsFraud' =>$monthIsFraud,
  17.                 'valueNotFraud'=>$monthIsNotFraud
  18.             ];
  19.         }
  20.         $isFraud=[];
  21.         $isNotFraud=[];
  22.         foreach ($monthlyTransaction as $item){
  23.             $isFraud[]=$item['valueIsFraud'];
  24.             $isNotFraud[]=$item['valueNotFraud'];
  25.         }
  26.  
  27.         $params=[
  28.             'isFraud'=>$isFraud,
  29.             'notFraud'=>$isNotFraud
  30.         ];
  31.  
  32.         return response()->json($params);
  33.  
  34.     }
  35.  
  36.     public function showUserTransactionByIp($userId)
  37.     {
  38.         $transaction=Transaction::select('ip_address')
  39.             ->where('user_id',$userId)
  40.             ->groupBy('ip_address')
  41.             ->get();
  42.  
  43.         $isFraud=[];
  44.         $isNotFraud=[];
  45.         $ipAddress=[];
  46.         foreach ($transaction as $item){
  47.             $ipAddress[]=$item->ip_address;
  48.             $IpIsFraud=Transaction::where(['ip_address'=>$item->ip_address,'user_id'=>$userId])
  49.                 ->where('is_fraud',1)->count();
  50.             $IpNotFraud=Transaction::where(['ip_address'=>$item->ip_address,'user_id'=>$userId])
  51.                 ->where('is_fraud',0)->count();
  52.             $isFraud[]=$IpIsFraud;
  53.             $isNotFraud[]=$IpNotFraud;
  54.         }
  55.  
  56.         $params=[
  57.             'ipAddress'=>$ipAddress,
  58.             'isFraud'=>$isFraud,
  59.             'notFraud'=>$isNotFraud
  60.         ];
  61.  
  62.         return response()->json($params);
  63.     }
  64.  
  65.     private function generateMonth($idx)
  66.     {
  67.         if(strlen($idx) < 2){
  68.             $idx = '0'.$idx;
  69.         }
  70.         return $idx;
  71.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement