Laravel Eloquent ORM-复杂的查询 [英] Laravel Eloquent ORM - Complex where queries

查看:450
本文介绍了Laravel Eloquent ORM-复杂的查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下查询:

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

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

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.

谢谢!

推荐答案

这将是您所需要的查询

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

但是我发现由于这两个组中都存在domainId条件,因此可以对其进行一些优化:

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

这篇关于Laravel Eloquent ORM-复杂的查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆