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

2 Answers 2

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

}
Sign up to request clarification or add additional context in comments.

1 Comment

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 ?
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.