'Laravel Schedule daily notification for each user on their preferred time
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();
Solution 1:[1]
I ended up finding this package which has convenient solutions for my situation: Laravel Snooze
Solution 2:[2]
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);
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Ali Jouadi |
| Solution 2 | Laisender |
