How can I run time event in laravel 8 in given date and time from mysql

120

Question: How can I run time event in laravel 8 in given date and time from mysql

I want to give a date and time on my schedule and when that date and time come to execute some function in my example here how is looks my Kernel.php

    protected function schedule(Schedule $schedule) {     $events = Event::all();         foreach($events as $event){         $schedule->job(new ListenEvent)         ->when(function() use ($event){             $now = date('Y-m-d H:i:s');             if(strtotime($now) == strtotime($event->event_date)){                 return true;             }else{                 return false;             }         })         ->onSuccess(function(Stringable $output){             echo "Event sent successfully ". $output;         })->onFailure(function(Stringable $output){             echo "Event failed to sent: " . $output;         });     } } 

Here is my job file ListenEvent.php what need to do

public function handle()     {         $events = Event::all();         $users = User::all();                  foreach($users as $user){             foreach($events as $event){                 $data = json_encode(                     array(                         'messages' => array(                             'channel' => 'sms',                             'to' => `$user->user_phoneNumber`,                             'content' => `$event->event_name`                         ),                     )                 );                  sendSMS($data);             }         }              } 

How can I achieve sending messages at a given time for example now in my DB date and time on task to run is inserted like this "2022-03-24 17:45:00"

Total Answers: 1

74

Answers 1: of How can I run time event in laravel 8 in given date and time from mysql

$events = Event::where('event_date',now()->format('Y-m-d H:i:00'))->get();  $events->each(function($event) use($schedule) {         $schedule           ->job(new ListenEvent)           ->onSuccess(function(Stringable $output){             echo "Event sent successfully ". $output;           })           ->onFailure(function(Stringable $output){             echo "Event failed to sent: " . $output;           }); }