我在数据库中有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;
我的问题是:
- 如果我把返回
$netsalesdata
在for循环中,我得到的第一原料只有(1支只) - ,如果我把它放在循环外,我得到的最后一行(只有第二分支),而我的数据库中有2个分支
如果DD($分支机构);你有两个分支吗? – Onix