Skip to content
Advertisement

PHP how to use OR in the right way

I have simple part of code to detect type of Discount, i added another type, but how to use operator OR (||) right way in this case ? ->where('taken', "N") or "R" do something. this wont work ->where('taken', "N" || "R")

$code = Discount::where('code',$discount_code)
        ->where('expiry','>',Carbon::now()->toDateString())
        ->where('subscription', $subscription)
        ->where('taken', "N")
        ->first();

Advertisement

Answer

in this case use whereIn() and pass array to second parameter

-where('taken', "N" || "R")

to

whereIn('taken', ["N","R"])

so your final code will be

$code = Discount::where('code',$discount_code)
        ->where('expiry','>',Carbon::now()->toDateString())
        ->where('subscription', $subscription)
        ->whereIn('taken', ["N","R"])
        ->first();

ref link https://laravel.com/docs/7.x/queries#where-clauses

User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement