How to write a query like select * from table where userid=parentid and age>30? I didn't find this usage in the documentation.
阿神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 is the standard answer.
Let me add, why is this happening.
Your requirement is that you need to reference parentid as the rvalue of where. However, in laravel, the rvalue of where cannot be sql, but can only be regular data structures, such as strings, values, time, etc. It cannot be a field name (users.id
) or something with SQL function. This is to prevent SQL injection.
So, you can only use the raw method mentioned by @ty0716 to execute native sql.