![](/img/trans.png)
[英]How to use query builder or raw SQL within a Laravel Eloquent relationship
[英]how to use raw sql with laravel eloquent builder
我需要從用戶表中獲取名稱和ID,名稱位於first_name
和last_name
兩列中。 這就是我所做的
$user = User::select('id',DB::raw("CONCAT(`first_name`, ' ' ,`last_name`) AS name"))
->where('name', 'like' ,$token)
這給我一個錯誤Unknown column 'name' in 'where clause'
。 我打印出了看起來像這樣的原始sql
select `id`, CONCAT(`first_name`, ' ' ,`last_name`) AS name from `user` where `name` like ? and `user`.`deleted_at` is null
如何正確使用concat和喜歡的雄辯構建器?
嘗試這個:
$user = User::select('id',DB::raw("CONCAT(`first_name`, ' ' ,`last_name`) AS name"))
->where(DB::raw("CONCAT(`first_name`, ' ', `last_name`)"), 'LIKE', "%".$token."%");
嘗試像那樣使用
$user = User::select('id',DB::raw("CONCAT(first_name, ' ' ,last_name) as name"))
->where(DB::raw("CONCAT(first_name, ' ' ,last_name)"), 'like' ,"%".$token."%")
您應該嘗試這樣:
$user = User::select('Tenant_Id', DB::raw('CONCAT(first_name, " ", last_name) AS name'))
->where('name', 'like' ,$token)
嘗試這個-
$user = User::select('id',DB::raw("CONCAT(`first_name`, ' ' ,`last_name`) AS name"))
->where(DB::raw("CONCAT(`first_name`,' ', `last_name`)"), 'LIKE', "%" . $name . "%");
它為我工作。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.