错误从所有记录中添加钱 - count()

error add money from all records - count()

我需要把“ingresos”栏中的所有钱加起来。

错误:属性 [presupuesto] 在此集合实例上不存在。

使用了变量 :

{{ $contador->presupuesto->count() }}

数据库:

控制器:

public function contador(){

        if (Auth::guest()) return redirect('/login');

      

        $contador = \App\Models\Registro::All();
       
        return view('contador',compact('contador'));
   


    }

我需要增加收入

您可以按照文档 https://laravel.com/docs/8.x/queries#aggregates 进行此类交易。

关于你的问题,你应该使用sum函数。

$contador->sum('presupuesto')

您计算了这些值,但您必须将它们相加。要计算值的总和,请使用:

$contador->sum('presupuesto');

你明白了 Error: Property [presupuesto] does not exist on this collection instance.

因为到 \App\Models\Registro::All() 你会得到一个集合,你需要迭代它来获取它的属性。 count() 是当您想要获取集合中的行数时。但是你想得到总和,正如其他答案已经提到的那样,你需要使用 sum().