Laravel Concat在查询中(条件) [英] Laravel concat in query (where condition)

查看:1887
本文介绍了Laravel Concat在查询中(条件)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对laravel(今天开始)很陌生,在构建简单查询时遇到了问题:

I am pretty new to laravel (started today) and facing a problem building a simple query:

$query->orWhere("CONCAT(`nvp`, ' ', `vpv`)", 'LIKE', "%$this->searchNeedle%");

上面的这一行是封装查询条件中的几个条件之一.我认为在这种情况下其他行不是必需的,但请告诉我是否需要查看它们.

我发现开发人员决定添加

I found out that the developer decided to add a

`

第一个orWhere/where参数的前后,这会导致我无法使用简单的concat,因为上面的行将导致:

before and after the first orWhere/where param which cause the problem that I cant use a simple concat, because the line above will result in:

`CONCAT(`vpv`, ' ', `nvp`)` LIKE ?)' 
↑                         ↑
this                    & this

因为这是自动添加的,所以我不能删除它而不会覆盖我不会的laravel-core函数.是否存在处理这两个`的基于SQL的"hack"?就像1 = 1一样吗?

Since this is automagically added i cant remove it without overwriting a laravel-core function which i wont. Is there any SQL-based "hack" that handles these two ` ? Something in the way like 1 = 1, you know?

也许我还有另一种解决方案来获得预期的结果,将一个字符串与两行组合在一起进行比较?

Maybe you have another solution for me to get the intended result, comparing one string with two rows in combination?

推荐答案

Laravel在后台做一些事情,例如为您添加刻度线.

Laravel does some stuff behind the scenes like adding in the tick marks for you.

幸运的是,它还提供了一些工具来帮助您完成工作...

Fortunately, it also offers a couple of tools to still get the job done for you...

对于这种类型的东西,DB::raw()通常效果很好.尝试这样的事情...

For this type of thing, DB::raw() usually works really well. Try something like this...

$query->orWhere(DB::raw("CONCAT(`nvp`, ' ', `vpv`)"), 'LIKE', "%".$this->searchNeedle."%");

这篇关于Laravel Concat在查询中(条件)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆