Laravel Eloquent ORM - 复杂的查询

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29036959/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 11:08:02  来源:igfitidea点击:

Laravel Eloquent ORM - Complex where queries

phpdatabaselaravelormwhere

提问by Ronald Jonkers

I have the following query:

我有以下查询:

DB::select("SELECT * FROM mod_dns_records WHERE (scheduled = 'N' AND scheduleTime = 0 AND domainId = {$id}) OR (deleteRow = 'Y' AND domainId = {$id})");

However, this is not safe against SQL injection. Could someone help me to make this safe, or tell me how to rebuild this with the ORM.

但是,这对于 SQL 注入是不安全的。有人可以帮助我确保安全,或者告诉我如何使用 ORM 重建它。

Thanks!

谢谢!

回答by lukasgeiter

This would be the query as you had it

这将是您拥有的查询

$result = DB::table('mod_dns_records')
            ->where('scheduled', 'N')
            ->where('scheduleTime', 0)
            ->where('domainId', $id)
            ->orWhere('deleteRow', 'Y')
            ->where('domainId', $id)
            ->get();

However I noticed it can be optimized a bit since the domainIdcondition exists in both groups:

但是我注意到它可以优化一点,因为domainId条件存在于两个组中:

$result = DB::table('mod_dns_records')
            ->where('domainId', $id)
            ->where(function($q){
                $q->where('scheduled', 'N');
                $q->where('scheduleTime', 0);
                $q->orWhere('deleteRow', 'Y');
            })
            ->get();