Laravel Eloquent ORM - Complex where queries

谁说胖子不能爱 提交于 2019-12-22 09:25:44

问题


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.

Thanks!


回答1:


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:

$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();


来源:https://stackoverflow.com/questions/29036959/laravel-eloquent-orm-complex-where-queries

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!