【问题标题】:How to concatenate a columns in Laravel Eloquent? [duplicate]如何连接 Laravel Eloquent 中的列? [复制]
【发布时间】:2020-03-13 20:20:31
【问题描述】:

考虑这个具有以下列的客户表:

  • 名字
  • 中间名
  • sur_name
  • 分机

因此:

id       first_name      middle_name     sur_name      extension
------------------------------------------------------------------
1        John            null            Doe            null
2        Harry           Noah            Potter         null
3        Sam             Smith           Doe            III
4        Hero            null            Finch          null

我做了这个原始查询:

$customer = DB::select("select c.* from customers c where concat_ws(', ' ,c.sur_name,concat_ws(' ',concat_ws(' ',c.first_name,c.middle_name),c.extension)) like ?", [$item]);

如何将其转换为模型库,如 Customer::where()->first() 并处理空值(concat_ws,在 mysql 的情况下)?

【问题讨论】:

  • 我认为它没有处理我数据库中的空值。
  • 如果您使用该链接中建议的属性 getter,您将提取所有数据(无论是否为空),然后使用模型属性进行连接。

标签: laravel eloquent


【解决方案1】:

试试下面的代码。

 $costomers = Customer::select(DB::raw("CONCAT('first_name','last_name') AS Name"))->get();

希望你能帮忙!!

【讨论】:

    猜你喜欢
    • 2014-03-27
    • 2018-01-08
    • 2019-10-24
    • 1970-01-01
    • 2014-10-18
    • 1970-01-01
    • 2019-06-28
    • 2019-12-06
    • 2013-01-21
    相关资源
    最近更新 更多