-1

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?

2
  • create a trait having a common code of controller and cronjob and import that in both classes. Commented Sep 2, 2022 at 10:36
  • Yes, I agree with Bhaumik that the problem is not how to call a controller method. It is that you should not call a controller method from other places. So the code should be extraced. Maybe a trait, maybe a job/action. Commented Sep 2, 2022 at 10:44

1 Answer 1

1

You need a Service Layer in your application to do this. Nice example here.

You must:

  • Move logic from Controller to Service
  • Call this service in Controller
  • Call this service in sheduler.
Sign up to request clarification or add additional context in comments.

1 Comment

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!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.