我有以下查询:
DB::select("SELECT * FROM mod_dns_records WHERE (scheduled = 'N' AND scheduleTime = 0 AND domainId = {$id}) OR (deleteRow = 'Y' AND domainId = {$id})");
但是,这对于 SQL 注入(inject)并不安全。有人可以帮助我确保它安全,或者告诉我如何使用 ORM 重建它。
谢谢!
最佳答案
这将是您所拥有的查询
$result = DB::table('mod_dns_records')
->where('scheduled', 'N')
->where('scheduleTime', 0)
->where('domainId', $id)
->orWhere('deleteRow', 'Y')
->where('domainId', $id)
->get();
但是我注意到它可以优化一点,因为 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();
关于php - Laravel Eloquent ORM - 复杂的 where 查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29036959/