2017-03-06 4 views
0

데이터베이스에 브랜드 1 개와 분기 2 개가 있으며, 각 분기에 대한 판매 데이터가 나타납니다.Laravel for 루프는 배열의 데이터를 반환합니다.

public function getCurrentSales($brandid){ 
$branches = DB::table('gc_branch')->where('BRAND_ID', $brandid) 
            ->select('BRANCHID', 'BRANCHNAME') 
            ->get(); 

for ($i=0; $i<count($branches);$i++){ 
$mtdnetsales= DB::table('st_sales') 
//query 
->select(DB::raw('sum(AMOUNT) as TOT')->get(); 

$ytdnetsales= DB::table('st_sales') 
//query 
->select(DB::raw('sum(AMOUNT) as TOT')->get(); 


$netsalesdata=[['BRANCHID' => $branches[$i]->BRANCHID, 'BRANCHNAME' =>branches[$i]->BRANCHNAME, 'MTDNETSALES' =>$mtdnetsales[0]->TOT, 'YTDNETSALES' =>$ytdnetsales[0]->TOT]]; 

}//end for 

return $netsalesdata; 

내 문제는 다음과 같습니다

내가 for 루프에 반환 $netsalesdata를 넣어 경우
  • , 나는 처음 원료만을 얻을 수 (1 분기에만 해당)
  • 내가 루프 밖에서 넣어 경우, 난, 내 데이터베이스가있는 동안 2 가지
+0

당신이 DD 경우 ($ 가지); 너는 두 가지를 모두 얻나요? – Onix

답변

1

변경이 netste (제 2 분 만 해당) 마지막 행을 얻을 이것에 llar (그리고 루프의 내부에 보관) :

$netsalesdata[$i]=[['BRANCHID' => $branches[$i]->BRANCHID, 'BRANCHNAME' =>branches[$i]->BRANCHNAME, 'MTDNETSALES' =>$mtdnetsales[0]->TOT, 'YTDNETSALES' =>$ytdnetsales[0]->TOT]]; 

을이 반환 : 추가 할

return $netsalesdata[]; 
+0

감사합니다. –

0
public function getCurrentSales($brandid) { 
    $branches = DB::table('gc_branch')->where('BRAND_ID', $brandid) 
        ->select('BRANCHID', 'BRANCHNAME')->get(); 

    for ($i=0; $i<count($branches);$i++){ 
     $mtdnetsales= DB::table('st_sales') 
       ->select(DB::raw('sum(AMOUNT) as TOT')->get(); 

     $ytdnetsales= DB::table('st_sales') 
       ->select(DB::raw('sum(AMOUNT) as TOT')->get(); 

     $netsalesdata[] =[ 
      'BRANCHID' => $branches[$i]->BRANCHID, 
      'BRANCHNAME' =>branches[$i]->BRANCHNAME, 
      'MTDNETSALES' =>$mtdnetsales[0]->TOT, 
      'YTDNETSALES' =>$ytdnetsales[0]->TOT]; 

    }//end for 

    // get size of the array 
    $records = count($netsalesdata); 
    // To get last record 
    print_r($netsalesdata[$records -1]); 
} 
0

사용을 array_push 기능을 새로운 변수 :

public function getCurrentSales($brandid){ 
    $netsalesdata= []; 
    $branches = DB::table('gc_branch')->where('BRAND_ID', $brandid) 
             ->select('BRANCHID', 'BRANCHNAME') 
             ->get(); 

    for ($i=0; $i<count($branches);$i++){ 
     $mtdnetsales= DB::table('st_sales') 
     //query 
     ->select(DB::raw('sum(AMOUNT) as TOT')->get(); 

     $ytdnetsales= DB::table('st_sales') 
     //query 
     ->select(DB::raw('sum(AMOUNT) as TOT')->get(); 


     array_push($netsalesdata, ['BRANCHID' => $branches[$i]->BRANCHID, 'BRANCHNAME' =>branches[$i]->BRANCHNAME, 'MTDNETSALES' =>$mtdnetsales[0]->TOT, 'YTDNETSALES' =>$ytdnetsales[0]->TOT]); 

    }//end for 

    return $netsalesdata; 
} 
관련 문제