【问题标题】:Concatenate two columns in eloquent where clause in laravel在laravel中雄辩的where子句中连接两列
【发布时间】:2019-01-08 03:59:47
【问题描述】:

在我的数据库中,country_codephone_number 是两个不同的字段。用户正在输入一个包含国家代码和电话号码的字符串来登录。为了验证这一点,我必须在雄辩的 where 子句中连接列 country_codephone_number

有人能告诉我怎么做吗?我在下面给出我的查询。

$user  = self::where('phone_number', '=', $username)->get()->first();

【问题讨论】:

    标签: php mysql laravel eloquent lumen


    【解决方案1】:

    您可以将 whereRaw 与原始 SQL 表达式一起使用,并将参数与第二个参数绑定。

    whereRaw("CONCAT(`country_code`, `phone_number`) = ?", [$username]);
    

    【讨论】:

      【解决方案2】:

      试试下面的代码:

      $user = self::whereRaw("CONCAT_WS(' ',`country_code`, `phone_number`) = ? ",  [$username])->get()->first();
      

      第一个参数应该是粘合片。

      【讨论】:

      • 永远不要将用户来源的变量放在原始表达式中!这为 SQL 注入打开了大门。
      • 好吧,我错了。
      猜你喜欢
      • 1970-01-01
      • 2018-05-19
      • 2021-02-13
      • 2014-01-03
      • 1970-01-01
      • 2018-09-03
      • 2014-11-05
      • 1970-01-01
      • 2014-09-20
      相关资源
      最近更新 更多