How to write two records with equal field values ​​using Eloquent outside Laravel?
曾经蜡笔没有小新
曾经蜡笔没有小新 2017-05-16 16:52:34
0
4
428

How to write a query like select * from table where userid=parentid and age>30? I didn't find this usage in the documentation.

曾经蜡笔没有小新
曾经蜡笔没有小新

reply all(4)
阿神

Method 1

$results = Model::whereRaw('userid=parentid and age>30')->get();

Method 2

$results = DB::table('table_name')->whereRaw('userid=parentid and age>30')->get();
仅有的幸福

@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.

过去多啦不再A梦

No one knows?

PHPzhong
$model->where('userid','parentid')->where('age','>',30)->get();
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template