Laravel - select 临时列 where 临时列
Laravel - select temporary column where temporary column
$search_str = "full name search";
$user = App\User::selectRaw("CONCAT(`f_name`, `l_name`) AS `fullname`")
->where('fullname', 'LIKE', '%$search_str%')
->get();
根据上面的代码,fullname
列实际上不存在于数据库 Table 中。 fullname
列只是一个临时列。
但是当我在 where
子句上使用它时,Laravel return 出现错误:
Column not found: 1054 Unknown column 'fullname' in 'where clause'
您可以在 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();
使用 Having 作为别名。请参阅下面的参考 link.
https://laravel.com/docs/5.6/queries#ordering-grouping-limit-and-offset
$search_str = "full name search";
$user = App\User::selectRaw("CONCAT(`f_name`, `l_name`) AS `fullname`")
->where('fullname', 'LIKE', '%$search_str%')
->get();
根据上面的代码,fullname
列实际上不存在于数据库 Table 中。 fullname
列只是一个临时列。
但是当我在 where
子句上使用它时,Laravel return 出现错误:
Column not found: 1054 Unknown column 'fullname' in 'where clause'
您可以在 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();
使用 Having 作为别名。请参阅下面的参考 link.
https://laravel.com/docs/5.6/queries#ordering-grouping-limit-and-offset