本文整理汇总了PHP中Illuminate\Support\Collection::sum方法的典型用法代码示例。如果您正苦于以下问题:PHP Collection::sum方法的具体用法?PHP Collection::sum怎么用?PHP Collection::sum使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Illuminate\Support\Collection
的用法示例。
在下文中一共展示了Collection::sum方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: testPipe
public function testPipe()
{
$collection = new Collection([1, 2, 3]);
$this->assertEquals(6, $collection->pipe(function ($collection) {
return $collection->sum();
}));
}
示例2: testGettingSumFromEmptyCollection
public function testGettingSumFromEmptyCollection()
{
$c = new Collection();
$this->assertEquals(0, $c->sum('foo'));
}
示例3: totals
public function totals($period, Carbon $from, Carbon $to = null)
{
switch ($period) {
case 'week':
$y_axis = DB::raw("DAY(created_at) as {$period}");
break;
case 'month':
$y_axis = DB::raw("DAY(created_at) as {$period}");
break;
case 'year':
$y_axis = DB::raw("MONTH(created_at) as {$period}");
break;
case 'day':
default:
$y_axis = DB::raw("HOUR(created_at) as {$period}");
break;
}
$prd = $this->data->select([DB::raw('count(id) as `count`'), $y_axis, 'type'])->groupBy($period)->groupBy('type')->get();
$colle = new Collection($prd);
return ['in' => $colle->sum(function ($x) {
return $x->type == 'in' ? $x->count : 0;
}), 'out' => $colle->sum(function ($x) {
return $x->type == 'out' ? $x->count : 0;
})];
}