dimanche 20 septembre 2020

PHP Laravel - conditions first check if the value is in the database then whether it is

Before the customer pays, he can enter the code for the discount. The discount is automatically 5% hardcoded. But now I need to set the discount from 0-100% this value is stored with the discount code in the database. What might the condition look like first check if there is a discount code in the database if not use hardcoded 5% discount?

database table dicount field code is PMX-123456 discont value field discount is 1-100%.

this line of code gives the value of the discount 5%

$response['discount'] = 5;

my goal is if $discount_code PMX-123456 is in database instead of PMX-getIdByHash which is hardcoded ->$response=['discount'] = $code->discount; instead of $response['discount'] = 5;

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

if (strpos($discount_code, 'PMX-') !== false) {
        $referal_uid = $this->usersRepo->getIdByHash($discount_code);
        $userExists = User::find($referal_uid);
        $different_user = ($referal_uid !== Auth::id() ? true : false);
        if ($userExists && $different_user)
        {
            $response['valid'] = true;
            $response['discount'] = 5;
            $response['referal'] = $userExists->email;
        }

            return $response;
        }
        return json_encode($response);

Aucun commentaire:

Enregistrer un commentaire