-
扬帆大鱼
在这种情况下,使用whereIn()数组并将其传递给第二个参数-where('taken', "N" || "R")到whereIn('taken', ["N","R"])所以你的最终代码将是$code = Discount::where('code',$discount_code)
->where('expiry','>',Carbon::now()->toDateString())
->where('subscription', $subscription)
->whereIn('taken', ["N","R"])
->first();参考链接https://laravel.com/docs/7.x/queries#where-clauses
-
大话西游666
您可以使用 的另一个功能->where,即它接受回调,这将生成一个“嵌套”函数:Discount::where('code',$discount_code)
->where('expiry','>',Carbon::now()->toDateString())
->where('subscription', $subscription)
->where(function($q) use($abc) {
$q->where('taken', "N")
->orWhere('taken', "R");
})->get();如果您对 SQL 感到满意,$q->where('taken', "N")->orWhere('taken', "R");将被包装在 上( ... ),这样您就可以使用 SQLWHERE expiry > *a date* AND subscription = *a value* and (taken = 'N' OR taken = 'R')
-
慕慕森
为了避免使用 的不稳定布尔逻辑expiry > x and taken = N or taken = R,如果采取的是 R 并忽略到期,则 will 结果为 true。您可以回电以在您的条件周围添加括号。->where(function ($query) {
$query->where('taken', 'N')
->orWhere('taken', 'R');
});这将产生与此类似的查询。这应该给出预期的结果。expiry > x and (taken = N or taken = R)
-
慕工程0101907
或者可以使用Where方法 $code = Discount::where('code',$discount_code)
->where('expiry','>',Carbon::now()->toDateString())
->where('subscription', $subscription)
->where('taken', "N")
->orWhere('taken', "R")
->first();