File: /mnt/data/ghayatcom/ghayatcom-api/app/Library/CouponLibrary.php
<?php
namespace App\Library;
use App\Coupon;
use Illuminate\Support\Str;
class CouponLibrary {
public function generateRandomCodes($coupon, $number, $type,$id) {
if(empty($coupon))
$code = Str::random($number);
else
$code = $coupon;
if($type == 'insert')
$affected = Coupon::where('code', $code)->get();
else
$affected = Coupon::where('code', $code)->where('id','!=',$id)->get();
if ($affected->count() > 0) {
if(!empty($coupon)) {
return 'coupon_code_exceed';
}
else
$this->generateRandomCodes($coupon, $number);
}
return $code;
}
}