Php 如何获得for循环laravel中的数字之和
我有一个循环,每一步我都能得到当天的价格。现在我想得到所有天数的总和,并将其显示给用户。以下是我的尝试:Php 如何获得for循环laravel中的数字之和,php,mysql,laravel,Php,Mysql,Laravel,我有一个循环,每一步我都能得到当天的价格。现在我想得到所有天数的总和,并将其显示给用户。以下是我的尝试: for($f=0;$f其中日期('from_date',''$to_date) ->orderBy('created_at','asc') ->采取(1) ->get(); //$sum_price+=数组_sum($data[$f]->销售价格) //还是这个 //$sum_price+=$data[$f]; $data\U array[]=$data[$f]->销售价格。'=======
for($f=0;$f其中日期('from_date',''$to_date)
->orderBy('created_at','asc')
->采取(1)
->get();
//$sum_price+=数组_sum($data[$f]->销售价格)
//还是这个
//$sum_price+=$data[$f];
$data\U array[]=$data[$f]->销售价格。'======='.$dates[$i].================';
}
但是每次我得到一天的价格,却没有得到所有天数的总和。我怎样才能做到这一点 您的代码非常混乱,没有多大意义 例如,为什么要对循环使用
?您的查询将始终返回相同的结果,那么循环的意义何在
什么是$i
由于查询总是返回一个结果->take(1)
,因此只有$data[0]
才会有值,因此$data[$f]
应该返回什么,例如$data[1]
,$data[2]
等等
您可以使用查询生成器对价格求和
$sum = Price::whereIn('id', $room_ids)
->whereDate('from_date', '<', $from_date)
->whereDate('to_date', '>', $to_date)
->orderBy('created_at', 'asc')
->sum('sales_price');
$sum=Price::其中('id',$room\u id)
->其中日期('from_date',''$to_date)
->orderBy('created_at','asc')
->总额(“销售价格”);
您的代码非常混乱,没有多大意义
例如,为什么要对
循环使用?您的查询将始终返回相同的结果,那么循环的意义何在
什么是$i
由于查询总是返回一个结果->take(1)
,因此只有$data[0]
才会有值,因此$data[$f]
应该返回什么,例如$data[1]
,$data[2]
等等
您可以使用查询生成器对价格求和
$sum = Price::whereIn('id', $room_ids)
->whereDate('from_date', '<', $from_date)
->whereDate('to_date', '>', $to_date)
->orderBy('created_at', 'asc')
->sum('sales_price');
$sum=Price::其中('id',$room\u id)
->其中日期('from_date',''$to_date)
->orderBy('created_at','asc')
->总额(“销售价格”);
您可以在Laravel Eloquent中使用求和函数。请查看详细信息
$sum_price = 0;
$data = Price::
whereIn('id',$room_ids)
->whereDate('from_date', '<', $from_date)
->whereDate('to_date', '> ', $to_date)
->orderBy('created_at', 'asc')
->sum('price');
$sum_price = $data[0];
echo $sum_price; // This will give you total price.
$sum\u price=0;
$data=价格::
其中('id',$room\u id)
->其中日期('from_date',''$to_date)
->orderBy('created_at','asc')
->金额(“价格”);
$sum_price=$data[0];
echo$sum_price;//这将给你总的价格。
您可以在Laravel Eloquent中使用求和函数。请查看详细信息
$sum_price = 0;
$data = Price::
whereIn('id',$room_ids)
->whereDate('from_date', '<', $from_date)
->whereDate('to_date', '> ', $to_date)
->orderBy('created_at', 'asc')
->sum('price');
$sum_price = $data[0];
echo $sum_price; // This will give you total price.
$sum\u price=0;
$data=价格::
其中('id',$room\u id)
->其中日期('from_date',''$to_date)
->orderBy('created_at','asc')
->金额(“价格”);
$sum_price=$data[0];
echo$sum_price;//这将给你总的价格。
试试这个:
$sum_price[] = $data[$f]->sales_price;
// here use this and outside of your loop do an array sum
$sum = array_sum($sum_price);
试试这个:
$sum_price[] = $data[$f]->sales_price;
// here use this and outside of your loop do an array sum
$sum = array_sum($sum_price);
$sum\u price=0;
$data=价格::
其中,'id',$room\u id中的'enter code here')
->其中日期('from_date',''$to_date)
->orderBy('created_at','asc')
->金额(“价格”);
$sum_price=$data[0];
echo$sum_价格;
$sum\u price=0;
$data=价格::
其中,'id',$room\u id中的'enter code here')
->其中日期('from_date',''$to_date)
->orderBy('created_at','asc')
->金额(“价格”);
$sum_price=$data[0];
echo$sum_价格;
请将样本表数据与预期结果一起添加到问题中。我不明白你想做什么。$data这意味着price模型有一个price属性,我想得到它的和。为什么你有for
循环?。循环内的查询将始终相同请将示例表数据与预期结果一起添加到问题中。我不明白你想做什么。$data这意味着price模型有一个price属性,我想得到它的和。为什么你有for
循环?。循环内的查询将始终相同