Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

shaddark's avatar

Laravel 9 - task scheduler controller function

Hello i'm looking on how to execute a controller function with a cronjob. I've read the documentation but it doesn't explain on how to call a controller function.

I have this function:

function oracle(Request $request)  {
// some get-insert statements using DB Facade

}

Let's say i want to run this function every 10 minutes for each db record that has status = 0

then in laravel scheduler something like:

 $schedule->call(function () {
   //.. DB query to check all orders that have status = 0
   //.. for each record that returns 0
   //.. execute oracle()
   }
})->everyMinute();

Basically all orders with status 0 are on mysql, now the oracle() logic triggers on a manual click. This function gets data from mysql and inserts to the oracle database. Once the record is on oracle it returns status 1 to mysql. I have to do this with a cron.

Which way should i go for?

0 likes
8 replies
Sinnbeck's avatar
Sinnbeck
Best Answer
Level 102

Dont call controllers from a schedule. Controllers are for http calls only. Instead put the logic into a job, and call that from both places (If the logic is identical)

shaddark's avatar

@Sinnbeck maybe it's not the best practice, but i created a command and insert the controller's logic in the handle() method. Everything seems working fine! Thank you so much!

thugic's avatar

To avoid repeating the same code all over, you could create a service class with a method called ‘oracle()’ and then do what you need to do in there

Then in controller and command you could do

// controller
public function oracle(Request $request)
{
    resolve(YourService::class)->oracle();
    // rest of your code here
}

// command
public function handle(YourService $yourService)
{
    $yourService->oracle();
}
shaddark's avatar

@thugic i will consider this way for avoid code repetition, thanks!

1 like

Please or to participate in this conversation.