Laravel Schedule daily notification for each user on their preferred time

975 Views Asked by At

I'm developing a task management app. I need to send a daily report notification at the user's preferred time. I did check Laravel features such as Task Scheduling and etc. But I don't find the right solution. If I want to use Schedule I have to write a foreach containing all with all my users and check if it's the time to notify, every single minute, which won't be efficient!

       //Not Efficient Code:
       $schedule->call(function (){
            foreach(User::all() as $user){
                if ($user->preferred_time == now()){
                    $user->notify(new Notification);
                }
            }
        }
        )->everyMinute();
2

There are 2 best solutions below

0
On BEST ANSWER

I ended up finding this package which has convenient solutions for my situation: Laravel Snooze

1
On

You could put the loop outside of the schedule but I don't see a possibility how that would make it more efficient:

foreach(User::all() as $user){

   $schedule->call(function() {
      // do stuff
   })->dailyAt($user->preferred_time);

}