要实现 select * from table where userid=parentid and age>30 这种查询应该如何写?没有在文档里找到这种用法.
阿神2017-05-16 16:54:34
$results = Model::whereRaw('userid=parentid and age>30')->get();
$results = DB::table('table_name')->whereRaw('userid=parentid and age>30')->get();
仅有的幸福2017-05-16 16:54:34
@ty0716 说的是标准答案。
我来补充一下,为什么会这样。
你的需求是,需要引用parentid作为where的右值,但是,在laravel中,where的右值,不能为sql,只能是常规的数据结构,例如字符串、数值、时间等。而不能是一个字段名(users.id
)之类的有sql功能的东西,这是为了防止sql注入。
所以,只能使用@ty0716所说的raw方法来执行原生的sql了。