0

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();
Ali Jouadi
  • 133
  • 1
  • 2
  • 15

2 Answers2

0

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);

}
Laisender
  • 714
  • 1
  • 5
  • 10
  • thank you for the effort, and where do you mean I should write the code, is this meant to be written in the schedule function of `App/Console/Kernel` ? – Ali Jouadi Apr 18 '22 at 14:23
0

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

Ali Jouadi
  • 133
  • 1
  • 2
  • 15