Search code examples
laravelnotificationssystemschedulephp-carbon

Laravel - Sending Notification when event starts


I developed a little Event System. By creating a new event the event startdate can be set on the future.

In this case I´m trying to send out a notification to a user, where is participant of this event, that the event started. How i can solve this?

I tried something like this with scheduling and an own ServiceProvider:

$challenges = Auth::User()->challenges()->where('startdate', '<=', Carbon::now())->get();
        foreach($challenges as $challenge) {

            Notifynder::category('challenge.started')
                ->from(1)
                ->to(1)
                ->url(url("challenge/$challenge->slug"))
                ->send();

        }

But yes, so the notification will send out everytime..not a good solution. I need them only one time when the event startdate switches from future to now.

Do you have an idea? Many thanks in advance!!

Regards


Solution

  • Personally I would just create a new field in database that would indicate if you sent the notifications. If you did not and the start date is after now you want to send notifications and set the flag to 1/true.

    Still, the solution you created is not the best/most performant, since now you'll query database at every single request. The better would be to create some CRON job to perodically check for such thing.

    For more information check this out: https://laravel.com/docs/5.2/scheduling

    This will provide you some basic informations about what you need to do to perodically make some actions.You may also want to read about Artisan Commands so you can run it every x minutes.