[英]how to use raw sql with laravel eloquent builder
I need to get name and id from my user table, name is in two column as first_name
and last_name
. 我需要从用户表中获取名称和ID,名称位于first_name
和last_name
两列中。 This is what I have done 这就是我所做的
$user = User::select('id',DB::raw("CONCAT(`first_name`, ' ' ,`last_name`) AS name"))
->where('name', 'like' ,$token)
which gives me an error Unknown column 'name' in 'where clause'
. 这给我一个错误Unknown column 'name' in 'where clause'
。 I printed out the raw sql which looks like this 我打印出了看起来像这样的原始sql
select `id`, CONCAT(`first_name`, ' ' ,`last_name`) AS name from `user` where `name` like ? and `user`.`deleted_at` is null
How can I use concat and like with my eloquent builder properly? 如何正确使用concat和喜欢的雄辩构建器?
Try This: 尝试这个:
$user = User::select('id',DB::raw("CONCAT(`first_name`, ' ' ,`last_name`) AS name"))
->where(DB::raw("CONCAT(`first_name`, ' ', `last_name`)"), 'LIKE', "%".$token."%");
try to use like that 尝试像那样使用
$user = User::select('id',DB::raw("CONCAT(first_name, ' ' ,last_name) as name"))
->where(DB::raw("CONCAT(first_name, ' ' ,last_name)"), 'like' ,"%".$token."%")
You should try this: 您应该尝试这样:
$user = User::select('Tenant_Id', DB::raw('CONCAT(first_name, " ", last_name) AS name'))
->where('name', 'like' ,$token)
Try this- 尝试这个-
$user = User::select('id',DB::raw("CONCAT(`first_name`, ' ' ,`last_name`) AS name"))
->where(DB::raw("CONCAT(`first_name`,' ', `last_name`)"), 'LIKE', "%" . $name . "%");
It worked for me. 它为我工作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.