简体   繁体   English

Laravel Eloquent ORM - 复杂的查询

[英]Laravel Eloquent ORM - Complex where queries

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. 但是,这对SQL注入是不安全的。 Could someone help me to make this safe, or tell me how to rebuild this with the ORM. 有人可以帮我安全,或告诉我如何使用ORM重建这个。

Thanks! 谢谢!

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 domainId condition 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();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM