Laravel-选择临时列,其中临时列

时间:2018-08-16 06:54:13

标签: laravel eloquent where

$search_str = "full name search";

$user = App\User::selectRaw("CONCAT(`f_name`, `l_name`) AS `fullname`")
        ->where('fullname', 'LIKE', '%$search_str%')
        ->get();

基于上面的代码,数据库表中实际上不存在列fullnamefullname列只是一个临时列。

但是当我在where子句上使用它时,Laravel返回错误:

  

未找到列:1054“ where子句”中的未知列“全名”

2 个答案:

答案 0 :(得分:2)

您可以在where过滤器中使用CONCAT函数。

$search_str = "full name search";

$user = App\User::selectRaw("CONCAT(`f_name`, `l_name`) AS `fullname`")
    ->whereRaw("CONCAT(`f_name`, `l_name`) LIKE '%?%'", [$search_str])
    ->get();

答案 1 :(得分:0)

使用Haveing作为别名。请参见下面的参考链接。

https://laravel.com/docs/5.6/queries#ordering-grouping-limit-and-offset