Sami
Sami

Reputation: 129

Notify users if their account is going to expire in 5 days later - Laravel

There are many users buying premium account in my website. How can i select users in which their account expiration day is exactly in 5 days later and notify them about renew account ?

in app\console\kernel.php add below function but dont know how to code about time in query.

/**
 * Define the application's command schedule.
 */

protected function schedule(Schedule $schedule)
{
    // $schedule->command('inspire')
    //          ->hourly();
    $schedule->call(function () {

        $users = User::where('expiration_time', '=', Carbon::now()->addDays(5))->get();

        //Notify related user about account emigrations
        Notification::send($users, new AccountExpiration());

    })->daily();
}

Upvotes: 1

Views: 2394

Answers (2)

Gabriel
Gabriel

Reputation: 970

Your code is correct, the only one thing that could cause your query from failing is your datebase table column type.

If your expiration_time column type is DATE you should use

Carbon::now()->addDays(5)->toDateString()

If your expiration_time column type is DATETIME you should use

Carbon::now()->addDays(5)

Hope this helps.

Upvotes: 5

Get the users in between today and 5 days later.

User::whereBetween('expiration_time', [Carbon::now(), Carbon::now()->addDays(5)])->get();

OR alternatively

User::whereBetween('expiration_time', [date("Y-m-d"), date("Y-m-d", strtotime("+5 days"))])->get();

Upvotes: 3

Related Questions