Home > Software engineering >  How to execute a scheduled function in Laravel, manually?
How to execute a scheduled function in Laravel, manually?

Time:08-01

I'm a bit lost in translation, English is not my primary language, so I might not be asking exactly what I mean, I'm sorry

This is my problem: I have a scheduled task that works perfectly, but all the logic is inside the function() in the $schedule->call(), I mean:

protected function schedule(Schedule $schedule)
{
  $schedule->call(function () {
    //
    // All my code goes here, around 25 lines, it calls an external API
    // and then stores the data on the database, it repeats every day at
    // 0700, timezone is not very relevant
    //
  })->dailyAt('07:00');
}

I want to do two things:

  1. How can I run the task "now", I mean by an Artisan command or something like that

  2. How can I run the tasks (the comments) on a seeder, one option would be to repeat the code, but I don't want to. Do I have to create an invokable controller? I think that might work but I'm not sure if that's the correct way to do it.

I know this looks quite basic, but I really don't know where to place the code so its executable both from the seeder and the scheduler, or even when clicking a button

  • Related