why not create a command to create a new code then call this once every 24 hours using the scheduler?
Laravel correct condition handling
I have a function on my site that creates a promo code for an affiliate automatically once every 24 hours. If 24 hours have passed since the creation of the promo code, it is deleted old promo from the database, and a new one is generated anew. But now there is a problem with this function, it generates a new promo code every time, regardless of whether 24 hours have passed or not.
My function:
public function autoGroupPromos()
{
$userList = Promo::get();
$userIds = $userList->pluck('user_id');
foreach ($userIds as $id) {
$date = Carbon::now();
$promoCodes = Promocode::query()->where('vk_user_id', '!=', null)->get();
foreach ($promoCodes as $promos) {
// If promo create 24 hours ago
$hours = $promos->created_at->diffInHours($date);
if ($hours >= 24) {
$promos->delete();
}
}
$permitted_chars = '0123456789abcdefghijklmnopqrstuvwxyz';
$code = substr(str_shuffle($permitted_chars), 0, 8);
Promocode::query()->create([
'name' => $code,
'sum' => '0.25',
'activates' => '100',
'vk_user_id' => $id
]);
$promoText = Promocode::where('vk_user_id', $id)->orderBy('created_at', 'desc')->first();
$promoName = $promoText->name;
$message = 'Your new promo: ' . $promoName . ';
$url = 'https://api.vk.com/method/messages.send';
$params = array(
'message' => $message,
'access_token' => 'token',
'v' => '5.81',
'peer_ids' => $id
);
$result = file_get_contents($url, false, stream_context_create(array(
'http' => array(
'method' => 'POST',
'header' => 'Content-type: application/x-www-form-urlencoded',
'content' => http_build_query($params)
)
)));
}
}
That is, if less than 24 hours have passed since the promo was created - it should not be deleted and a new promo code should not be generated. But now unfortunately there is an error somewhere.
What can be the problem?
Please or to participate in this conversation.