Reputation: 789
I'm doing a function in laravel, where my user registers a template message:
public function createTemplate(Request $request)
{
if(!($error = $this->isNotValidate($request))){
//Log::error(print_r("validado", true));
$message = Message::create($request->toArray());
//job that runs every day calling my sendMessag
$response = ['message' => $message];
return response($response, 200);
}
return response($error, 422);
}
I want that after registering this mangem, trigger a service that will run every day at the same time, then within this function I will implement my message trigger logic:
public function sendMessage()
{
$messages = Message::where('type', 'like', 'template')->get();
foreach ($messages as $message){
if(/*logic if send message or not*/){
$recive_ids = //get my recive_ids
$users = User::where('user_group', $recive_ids)->get();
Notification::send($users, new MessageNotification($message));
}
}
}
How do I create this job that runs every day calling my sendMessage function?
Upvotes: 1
Views: 999
Reputation: 620
You can use the Laravel Schedule Feature to run your function or any command.
First, you have to find a suitable method for your need.
After that, you have to update the schedule function which is available in app/Console/Kernel.php
->dailyAt('13:00');
As per your requirement, you need a scheduling method dailyAt().
Second, add the schedule:run
command in crontab to run every minute
* * * * * cd /path-to-your-project && php artisan schedule:run >> /dev/null 2>&1
You should refer to this document for more informationSchedule Document
Upvotes: 4