Laravel Eloquent ORM - 复杂的查询

Ron*_*ers 3 php database orm where laravel

我有以下查询:

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

但是,这对SQL注入是不安全的.有人可以帮我安全,或告诉我如何使用ORM重建这个.

谢谢!

luk*_*ter 10

这就是你所拥有的查询

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

但是我注意到它可以稍微优化一下,因为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();
Run Code Online (Sandbox Code Playgroud)