Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/74.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 作为值的总和(值)SQL查询不工作_Php_Html_Sql_Laravel - Fatal编程技术网

Php 作为值的总和(值)SQL查询不工作

Php 作为值的总和(值)SQL查询不工作,php,html,sql,laravel,Php,Html,Sql,Laravel,因此,我在clientPurchases类中有一个函数,它选择某个客户机在某个月份和年份的价值总和,如果它的空值返回0,则返回它 我的错误是显示总计的变量“total”返回的值错误 public function purchasesCount($month_id, $year_id) { $purchasesCount = ClientPurchases::select(DB::raw('SUM(value) AS value')) ->wh

因此,我在clientPurchases类中有一个函数,它选择某个客户机在某个月份和年份的价值总和,如果它的空值返回0,则返回它

我的错误是显示总计的变量“total”返回的值错误

public function purchasesCount($month_id, $year_id)
    {    
        $purchasesCount = ClientPurchases::select(DB::raw('SUM(value) AS value'))
        ->where('client_id', $this->id)
        ->whereYear('date', '=', $year_id)
        ->whereMonth('date', '=', $month_id)
        ->get();

        if(!empty($purchasesCount->value)){
            return $purchasesCount->value;
        }else{
            return 0;
        }
    }
然后在我的show.blade中,我试着做一个foreach,把所有的客户加在一张卡片上

<div class="col-md-4">
                <div class="carde card-1">
                    <h3>Clientes</h3>
                    <?php 
                        $total = 0;
                        foreach($clients as $client){
                           $valor = $client->purchasesCount($month, $year);
                           $total += $valor; 
                        }
                    ?>
                       
                    <h4 class="valor">{{ $total }} €</h4>
                    <h4 class="mes">{{date('F', mktime(0, 0, 0, $month - 1, 10))}}</h4>
                    <br>
                </div>
                </div>

客户
{{$total}}€
{{date('F',mktime(0,0,0,$month-1,10))}

我是拉拉维尔的新手,所以帮助将是grat tkx:)

您可以直接使用该方法:

 $purchasesCount = ClientPurchases::
            where('client_id', $this->id)
            ->whereYear('date', '=', $year_id)
            ->whereMonth('date', '=', $month_id)
            ->sum('value');
return $purchasesCount;
您可以直接使用以下方法:

 $purchasesCount = ClientPurchases::
            where('client_id', $this->id)
            ->whereYear('date', '=', $year_id)
            ->whereMonth('date', '=', $month_id)
            ->sum('value');
return $purchasesCount;

如何称呼我刀片上的“sumed”值@OMRIt将只返回您只需将上述代码粘贴到函数中并在循环中调用函数所需的值。如何在我的刀片上调用“sumed”值@OMRIt将只返回您只需将上述代码粘贴到函数中并在循环中调用函数所需的值。