Monday, March 6, 2017

Laravel for loop return data in in array

I have 1 brand and 2 branches in my database, I'm getting the sales data for each branch.

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;

my problem is : if I put the return $netsalesdata in the for loop, I get the first raw only (1 branch only), if I put it outside the loop, i get the last row(the second branch only) , while my database has 2 branches. Any help?



via Elio Chamy

Advertisement