1

I ask this question while I have done researches to find clear solution for this problem but many of available answers are just how to use Laravel Scheduler itself!

So I am creating a simple website with Laravel to let users create reminders for themselves.
Users can receive reminder either on their Emails or if they are logged in, they receive a notification alert something like Facebook Notifications.
I am not sure if I am following the right path or not but I'm using Laravel Scheduler to send reminders to each specific user.
I also use Events in the Laravel to push recent changes every 1 minute to users but my problem is, all users receive same notification. For example if I want to remind an appointment to Mr X, Ms Y also receives same exact notification!!!

At this point, I have two questions:
1 - Is using Laravel Scheduler a good idea for this project or not? If not then what technology or method is recommended?
2 - If I have to use Laravel Scheduler for this project, so how can I send notification to related user? (I cannot use User ID as Laravel Scheduler is running by server itself and not users!)
I also attached my codes to show you what I have up to this point and I know the method I used in Laravel Channels Broadcast is somehow wrong but I just tried it!
If anyone knows the answer please help me. Thank you
Laravel Scheduler - Command - Handle Function

public function handle()
    {
        $reminders = Note::whereTime('task_date', '<=', Carbon::now()->addMinutes(30))
        ->WhereTime('task_date', '>', Carbon::now()->subMinutes(30))->get();

        foreach ($reminders as $reminder) {
            if (!$reminder->notified) {
                Note::find($reminder->id)
                ->update(['notified' => 1]);
                event(new RemindUsers($reminder));
                dd($reminder->title);
            }
        }
    }

Laravel Event

public $reminder;
    
public function __construct($reminder)
    {
        $this->reminder = $reminder;
    }
    
    public function broadcastOn()
    {
        return new PrivateChannel('remind.'.$this->reminder->user_id);
    }

    public function broadcastWith () {
        return [
            'reminders' => $this->reminder
        ];
    }

Laravel Channels

Broadcast::channel('remind.{id}', function ($user, $id) {
    $notes = Note::where('user_id', $id)->get();
    foreach ($notes as $note) {
        return $user->id === $note->user_id;
    }
});

Vue JS Code - Echo Method

data () {
      return {
        user: 1
      }
    },
CatchReminders () {
        Echo.private(`remind.${this.user}`)
          .listen('RemindUsers', (response) => {
            this.$toast.info(`${response.reminders.title}`)
          })
      }
4
  • The scheduler is a good choice to do something at a specific time and date as long as you only need an accuracy level of minutes (it might not be an exact minute but within a couple of mins) Commented Dec 10, 2021 at 11:03
  • Please provide more info regarding how exactly you're targeting each user then if not via user data. Commented Dec 10, 2021 at 12:16
  • @DerickMasai In this case, I used Laravel Channels to filter users. I tried to use user ID from current session but it wasn't possible. And sorry I forgot to post Vue JS code. I updated post with my Vue JS codes. So I pass current User ID from Vue JS to Laravel Channels to filter coming data. However this is not a good practice but it somehow works however I just get 1 notification no matter how many data is sent by Scheduler and Events! Commented Dec 10, 2021 at 22:38
  • @apokryfos As reminders can be set to any date and time I think it has to check for all date and times every minute. However I am not sure if for this project, Scheduler is a good option or not. I didn't find any other options better than Scheduler in Laravel. Commented Dec 10, 2021 at 23:37

0

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.