I am trying to calculate two columns values but I am receiving the following error. The Minus between invoice and payment is a arithmetical operation. Can you please help me.
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 }}
Error
SQLSTATE[42S22]: Column not found: 1054 Unknown column 'sum(invoice-payment)' in 'field list' (SQL: select
sum(invoice-payment)
fromtb_accounts
)
You need a raw expression:
\DB::table('tb_accounts')->select(\DB::raw('sum(invoice)'))
But, I suppose you need to sum invoice
and payment
:
\DB::table('tb_accounts')->select(\DB::raw('(invoice - payment) AS amount'))
See the docs: https://laravel.com/docs/5.5/queries#raw-expressions
Note: avoid the minus
in your column name or use back-ticks (Do minus sign in mysql table column names cause issues?)
this will solve your problem
use Illuminate\Support\Facades\DB;
DB::table('tb_accounts')->select(\DB::raw('(invoice + payment) AS amount'))
You are getting this error htmlentities() expects parameter 1 to be string, array given (View: /home/unive/public_html/sistem/resources/views/accounts/index.blade.php)
because you are passing array in place of string if you need to pass array in your view try to return like this
return view('account.index')->withdata($data);
and then use in your index.blade.php
$data['balanceTotal'];