Laravel - 算术运算

时间:2017-10-24 08:22:47

标签: 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->data);
}

Index.blade.php

{{ $balanceTotal }}

错误

  

SQLSTATE [42S22]:找不到列:1054未知列   '和(发票支付)'在'字段列表' (SQL:选择   来自sum(invoice-payment))的tb_accounts

2 个答案:

答案 0 :(得分:1)

你需要一个原始表达式:

\DB::table('tb_accounts')->select(\DB::raw('sum(invoice)'))

但是,我想你需要总结invoicepayment

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

请参阅文档:https://laravel.com/docs/5.5/queries#raw-expressions

注意:避免列名中的minus或使用反向标记(Do minus sign in mysql table column names cause issues?

答案 1 :(得分:0)

这将解决您的问题

<Route path="/login" component={Login} />
      <Route path="/logout" component={Login} />

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

因为如果你需要在你的视图中传递数组,你正在传递数组来代替字符串尝试像这样返回


htmlentities() expects parameter 1 to be string, array given (View: /home/unive/public_html/sistem/resources/views/accounts/inde‌​x.blade.php)

然后在return view('account.index')->withdata($data);

中使用
index.blade.php