Search code examples
phplaravelcronscheduling

Laravel Cron Jobs - controller function


I have a controller in laravel project in /app/Http/Controllers. The controller called transferDataController.

<?php
namespace App\Http\Controllers;
use DB;
class TransferDataController extends Controller{
public function moveStTempSales(){



 // then a lot of queries.

  }
}

I need to call this function every 15 minutes. Without calling the api using url on a button or some elements. I was reading about Laravel Schedule. It works on Laravel 4? And if yes how can I schedule this function in my case ? I have a good knowledge in server cron jobs.


Solution

  • Go the following directory:

    yourproject/app/Console/Commands

    In this folder create you can create a class file that contains the function which is to be called on cron.

    Now on the same path there is a file : Kernel.php

    This file contains:

    $schedule->command();
    

    which is to be used to call the function of the currently created class.

    After that use:

    php artisan schedule:run
    

    to run the functionality.

    Reference