Laravel where_column1_and_column2 error? - php

I use laravel 4 PHP Framework when I write this method:
where_email_and_password($email, $password)
show this error:
SQLSTATE[42S22]: Column not found: 1054 Unknown column
'_email_and_password' in 'where clause' (SQL: select * from users
where _email_and_password = ? limit 1) (Bindings: array ( 0 =>
'email#example.com', ))
See code and error: http://i.stack.imgur.com/dn85M.png

Looks like you are migrating from Laravel 3 where this is a valid method call. In Laravel 4, everything follows the camel case convention. I have not tested this but looking at the source code for the method that handles dynamic wheres, try whereEmailAndPassword($email, $password)
See the method here: https://github.com/laravel/framework/blob/master/src/Illuminate/Database/Query/Builder.php#L747
Hope this helps.

Related

Laravel 5.8: SQLSTATE[42S22]: Column not found: 1054 Unknown column

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.

Laravel Query Builder weird column names conversion

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();

Difference between two dates in Yii's Relation

How can I perform a DATEDIFF function in CDbCriteria relations in yii
$criteria->with = array(
'company'=>array(
'condition'=>"user_type=2 AND approval_status='A'",
'select'=>'user_company'
),
'subscription'=>array(
'condition'=>"subscription_status='A'",
'select'=>'DATEDIFF(DATE(NOW()),DATE(subscription_startdate))'
)
);
I tried using the above code, Then got an error saying DATEDIFF not found in particular table.
So I add subscription relation as follows
'return array(
'company'=>array(self::BELONGS_TO,'Users',array('user_id'=>'user_id')),
'subscription'=>array(self::STAT,'SubscriptionDetails','subscription_startdate'),
);
Now also showing the below error
CDbCommand failed to execute the SQL statement: SQLSTATE[42S22]: Column not found:
1054 Unknown column 'Array' in 'field list'
Is this the right way to perform date difference in yii relations?
Thanks in Advance

Laravel 4 query builder - not null column not found

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.

Zend Framework 2 and SELECT count(*) query

I'm trying to do a query like this using Zend Framework 2:
SELECT count(*) as num FROM mytable
Here's the code I'm using to build my select statement (bear in mind I've imported the necessary classes):
$select = new Select();
$select->from('mytable')
->columns(array('num'=>'count(*)'), false);
This code doesn't work because the resulting query is as follows:
SELECT [count(*)] AS [num] FROM [mytable]
...which throws the following error:
Invalid column name 'count(*)'
This is caused by the square brackets around count(*). How can I get this to work properly, basically to have count(*) instead of [count(*)] in the SQL. Also, I know that you can do it with just a regular query, but I need this to work with the Select object. As far as I know, this used to work with the previous versions of Zend, I've seen plenty of solutions for those, but nothing for Zend Framework 2.
Somebody on another forum was kind enough to give me the answer for this. This is how it's done:
$select->columns(array('num' => new \Zend\Db\Sql\Expression('COUNT(*)')));
Yes, without new \Zend\Db\Sql\Expression('COUNT(*)'), just COUNT(*) leads to the following error statement:
SQLSTATE[42S22]: Column not found: 1054 Unknown column 'albs.COUNT(*)' in 'field list'
Having the
new \Zend\Db\Sql\Expression('COUNT(*)')
resolved it.
Could you try this code?
$this->num = $select->columns(array('num' => new \Zend\Db\Sql\Expression('COUNT(*)')));
return $this->num;

Categories