在客户付款之前,他可以输入折扣代码。折扣自动进行5%硬编码。但是现在我需要将折扣设置为0-100%,该值与折扣代码一起存储在数据库中。如果不使用硬编码的5%折扣,首先检查数据库中是否有折扣代码,情况看起来像什么?
数据库表dicount
字段code
是PMX-123456
不连续值字段discount
是1-100%。
这行代码给出了5%的折扣值
$response['discount'] = 5;
我的目标是数据库中是否有$discount_code
PMX-123456
而不是硬编码的PMX-getIdByHash
-> $response
= ['discount'] = $ code-> discount;
而不是$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);