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) from tb_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'];
Related
SQLSTATE[42883]: Undefined function: 7 ERROR: function sum(character varying) does not exist
LINE 1: select sum("amount") as aggregate from "payments"
^
HINT: No function matches the given name and argument types. You might need to add explicit type casts. (SQL: select sum("amount") as aggregate from "payments")
PDOException
SQLSTATE[42883]: Undefined function: 7 ERROR: function sum(character varying) does not exist LINE 1: select sum("amount") as aggregate from "payments" ^ HINT: No function matches the given name and argument types. You might need to add explicit type casts.
It's a Laravel Project and working fine locally, but throwing this error on Heroku, please kindly assist.
This is my code: $totalAmount = DB::table('payments')->sum('amount');
Its working fine locally.
For others facing this same issue.
Firstly, sum() does not accept varchar or string( if your column data type is varchar/string you will get that error ).
You can change your data type to integer or double( or the likes ).
Write you code like this $totalAmount = Payment::select(DB::raw('sum(cast(amount as double))'))->get(); ( remember get() will return and array )
or $totalAmount = DB::raw('SUM(amount)'); instead of $totalAmount = DB::table('payments')->sum('amount'); ( but you can write it like this if you will go for the first option to change data type)
For me I changed data type to integer and use this $totalAmount = DB::table('payments')->sum('amount');
I have a table called user_wallet like this:
Then at the Controller, I tried this:
try {
if ($request->pay_wallet == '1') {
$us_id = Auth()->user()->usr_id;
$user_wallet = UserWallet::find('user_id', $us_id);
dd($user_wallet);
}
}catch (\Exception $e) {
dd($e);
}
But I get this error:
SQLSTATE[42S22]: Column not found: 1054 Unknown column '2' in 'field list' (SQL: select 2 from user_wallet where user_wallet.id = user_id limit 1)
However as you can see in the picture, there are two wallets with the user_id of 2.
So what's going wrong here? How can I solve this issue?
the find method only works on id so it's better to use where clause
$user_wallet = UserWallet::where('user_id', $us_id);
I think you are using find() wrong, this method accepts only one paramater, it is the value of the primary key you want to find. Because you are in a pivot table and there is no id present,
you will have to use firstWhere('user_id', $us_id) for the first occurrence or you will have to rewrite the find() method for the pivot table.
But please be aware that a user could have multiple wallets, so it might be better to ask them which wallet to use.
i have a select wuery in my php/laravel code the problem is when i run it it gives this error
SQLSTATE[42S22]: Column not found: 1054 Unknown column '4' in 'where clause'
where (`element_value`.`p_id` = `4`))
this is my query
DB::table('value_tbl')
->select(DB::raw("CONCAT(values.value_name,'/',element_value.qan) AS full_value"))
->leftJoin('element_value','element_value.element_id','value.element_id')
->where (['element_value.p_id'=>$a->p_id])->get()->pluck('full_value')->toArray())
If you are to use an operator the where function takes three arguments: first the name of the column, second is an operator, and third is value to compare against.
Change your condition to appear as below
->where('element_value.p_id', '=', $a->p_id)
// or, because you are making an exact match, do this
->where('element_value.p_id', $a->p_id)
The code you have at the moment is getting confused because you are telling Laravel you are making a multiple where condition, through the use of an array as the where first argument.
Laravel then takes the value of that array and tries to convert it to column, operator, and value - the way it does for the above snippet.
If you really want to use an array, you'd need to do the following:
->where([
['element_value.p_id', '=', $a->p_id]
])
// or, because you are making an exact match, do this
->where([
['element_value.p_id', $a->p_id]
])
Notice how we passed two sets of arrays?
That's because Laravel either wants each argument separate, or an array-of-arrays containing the correct signature.
For a more detailed answer; the signature looks like this
public function where($column, $operator = null, $value = null, $boolean = 'and')
If $column is an array, it is assumed that an array-of-arrays is passed into the method, and each sub array, will effectively be spread across the above signature.
As you were passing a single array to the method, Laravel was taking the value of 4 and then using that as the $column.
This is because you used a key-value for that array, so the column name that you had used was actually being ignored.
The other interesting thing about this method, is that it has the following snippet:
// If the given operator is not found in the list of valid operators we will
// assume that the developer is just short-cutting the '=' operators and
// we will set the operators to '=' and set the values appropriately.
if ($this->invalidOperator($operator)) {
[$value, $operator] = [$operator, '='];
}
This allows us to not pass in an operator, and Laravel will just assume that we want an = to be used instead.
That's how I was able to omit it from my above examples.
I am trying to figure out why is DB::raw() queries are getting sent as lowercases?
Query = DB::('table_name')->select(['is_read as isRead'])->get();
Result = [{ isRead: true }]
But when I do raw its converting it to lowercase
Query = DB::('table_name')->select(DB::raw('is_read as isRead'))->get();
Result = [{ isread: true }]
I have a reason to use DB raw so I really need to figure this thing out.
I'm not able to reproduce the issue you mention... Which version of Laravel are you using?
For instance, if I run the following:
DB::table('users')->select(DB::raw('is_read as isRead'))->get();
I would get the error:
Illuminate/Database/QueryException with message 'SQLSTATE[42S22]: Column not found: 1054 Unknown column 'is_read' in 'field list' (SQL: select is_read as isRead from users)'
Which is normal because I have no is_read column in my users table. However, you can see in the error that the isRead is not converted to lowercase.
Maybe you can use selectRaw instead:
DB::table('table_name')->selectRaw('is_read as isRead')->get();
The query would be:
select is_read as isRead from `table_name`
Otherwise, could you update your question to provide more information on how to reproduce so your issue with the capitalization.
I can confirm that when using DB:raw (or orderByRaw or any row raw methods) column names get converted to lowercase.
To avoid that, you can try to protect the name with quotes:
Query = DB::('table_name')->select(['is_read as "isRead"'])->get();
Route::get('/test', function(){
return DB::table('table')->where_not_null('column')->get();
});
In Laravel 4 I have the following route, obviously with table and column replaced. I had it in the controller but it wasn't working, so I moved it to a test route.
The error I am getting is:
SQLSTATE[42S22]: Column not found: 1054 Unknown column '_not_null' in 'where clause' (SQL: select * from `table` where `_not_null` = column)
https://tower.la.utexas.edu/docs/database/fluent#where
Documentation says:
return DB::table('table')->where_null('column')->get();
It should be
return DB::table('table')->whereNull('column')->get();
Or
return DB::table('table')->whereNotNull('column')->get();
You have used where_not_null.This is the right reference.
See the proper writing for that: http://laravel.com/docs/queries#selects
DB::table('table')->whereNotNull('column')->get();
The documentation states that following the table declaration you should put the query builder information, where_null, where_not_null, where_in.
However that doesn't work. I'm not sure why, it makes no sense, and is frustrating. However, this does work.
return DB::table('user')->whereuserName('not_null')->get();
By placing the column name where the documentation says to put the query, and the query where the documentation says to put the column you are able to execute your query. To note, camel case is converted to snake case in the query, so the column has to be named user_name.
I am posting this because I attempted googling it and was not able to find anything.