84669 person learning
152542 person learning
20005 person learning
5487 person learning
7821 person learning
359900 person learning
3350 person learning
180660 person learning
48569 person learning
18603 person learning
40936 person learning
1549 person learning
1183 person learning
32909 person learning
How to write a query like select * from table where userid=parentid and age>30? I didn't find this usage in the documentation.
$results = Model::whereRaw('userid=parentid and age>30')->get();
$results = DB::table('table_name')->whereRaw('userid=parentid and age>30')->get();
@ty0716 说的是标准答案。
我来补充一下,为什么会这样。你的需求是,需要引用parentid作为where的右值,但是,在laravel中,where的右值,不能为sql,只能是常规的数据结构,例如字符串、数值、时间等。而不能是一个字段名(users.id)之类的有sql功能的东西,这是为了防止sql注入。
users.id
所以,只能使用@ty0716所说的raw方法来执行原生的sql了。
没人知道吗?
$model->where('userid','parentid')->where('age','>',30)->get();
方法一
方法二
@ty0716 说的是标准答案。
我来补充一下,为什么会这样。
你的需求是,需要引用parentid作为where的右值,但是,在laravel中,where的右值,不能为sql,只能是常规的数据结构,例如字符串、数值、时间等。而不能是一个字段名(
users.id
)之类的有sql功能的东西,这是为了防止sql注入。所以,只能使用@ty0716所说的raw方法来执行原生的sql了。
没人知道吗?