Php 拉威尔-算术运算

Php 拉威尔-算术运算,php,laravel,Php,Laravel,我试图计算两列值,但收到以下错误。发票和付款之间的负数是一个算术运算。你能帮帮我吗 Controller.php public function getIndex( Request $request ) { $this->data['balanceTotal'] = \DB::table('tb_accounts')->select('sum(invoice-payment)')->get(); return view('account.index',$this->dat

我试图计算两列值,但收到以下错误。发票和付款之间的负数是一个算术运算。你能帮帮我吗

Controller.php

public function getIndex( Request $request )
{
$this->data['balanceTotal'] = \DB::table('tb_accounts')->select('sum(invoice-payment)')->get();
return view('account.index',$this->data);
}
{{ $balanceTotal }}
Index.blade.php

public function getIndex( Request $request )
{
$this->data['balanceTotal'] = \DB::table('tb_accounts')->select('sum(invoice-payment)')->get();
return view('account.index',$this->data);
}
{{ $balanceTotal }}
错误

SQLSTATE[42S22]:未找到列:1054未知列 “字段列表”中的“金额(发票付款)”(SQL:选择
金额(发票付款)
来自
tb\u账户


您需要一个原始表达式:

\DB::table('tb_accounts')->select(\DB::raw('sum(invoice)'))
但是,我想您需要合计
发票
付款

\DB::table('tb_accounts')->select(\DB::raw('(invoice - payment) AS amount'))
见文件:


注意:避免列名中的
减号
,或使用反勾号()

您需要一个原始表达式:

\DB::table('tb_accounts')->select(\DB::raw('sum(invoice)'))
但是,我想您需要合计
发票
付款

\DB::table('tb_accounts')->select(\DB::raw('(invoice - payment) AS amount'))
见文件:


注意:避免列名中的
减号或使用反勾号()

这将解决您的问题

use Illuminate\Support\Facades\DB;
DB::table('tb_accounts')->select(\DB::raw('(invoice + payment) AS amount'))
您将收到此错误

htmlentities()希望参数1是字符串,数组给定(视图:/home/unive/public\u html/sistem/resources/views/accounts/inde‌​x、 blade.php)

因为要传递数组而不是字符串,所以如果需要在视图中传递数组,请尝试这样返回

return view('account.index')->withdata($data);
然后在
index.blade.php中使用

$data['balanceTotal'];

这会解决你的问题

use Illuminate\Support\Facades\DB;
DB::table('tb_accounts')->select(\DB::raw('(invoice + payment) AS amount'))
您将收到此错误

htmlentities()希望参数1是字符串,数组给定(视图:/home/unive/public\u html/sistem/resources/views/accounts/inde‌​x、 blade.php)

因为要传递数组而不是字符串,所以如果需要在视图中传递数组,请尝试这样返回

return view('account.index')->withdata($data);
然后在
index.blade.php中使用

$data['balanceTotal'];

您的DB表中是否有发票付款列?@NipunTyagi我有发票和付款列。不是发票付款栏看到我的答案。我根据您的评论更新了此内容是否在DB中的表中有发票付款列?@NipunTyagi我有发票和付款列。不是发票付款栏看到我的答案。我根据你的评论更新了这个。现在它说类'App\Http\Controllers\DB'未找到我用作\DB::table('tb_accounts')->select(DB::raw('invoice-payment)as amount')。最好导入名称空间:
use DB如果使用DB facade超过1次。请使用Illumb\Support\Facades\DB;或者使用DB??两者都是可能的;-)你好现在它说类'App\Http\Controllers\DB'未找到我用作\DB::table('tb_accounts')->select(DB::raw('invoice-payment)as amount')。最好导入名称空间:
use DB如果使用DB facade超过1次。请使用Illumb\Support\Facades\DB;或者使用DB??两者都是可能的;-)