here is how data is updating in the DB with the page refresh but i want it to update automaticaaly after every 5 minutes so that without refreshing my data becomes visables on welcome page as well as the data is inserted automatically on function call(that should be after every 5 minutes). i am a beginner i tried to learn from laravel documentation but its not exactly displaying what i need.. your help will be appriciated... here is the code of how i am inserting data in the DB:
foreach($files as $file){
if (Invoice::where('file_name', '=', $file['file_name'])->exists()) {
}
else {
$store = new Invoice;
$store->file_name = $file['file_name'];
$store->name = $file['customer_name'];
$store->po_no = $file['po'];
$store->address = $file['city'];
$store->total_items = count($file['products']);
$store->save();
$invid = $store->id;
for($x = 0; $x < count($file['products']); $x )
{
$pstore = new InvoiceProduct;
$pstore->product_name = $file['products'][$x];
$pstore->invoice_id = $invid;
$pstore->quantity =$file['count'][$x];
$pstore->barcode=$file['scan'][$x];
$pstore->vendor_code =$file['vn'][$x];
$pstore->save();
}
}
and the function name is index i want to schedule this function every 5 minutes.. Thank you!
CodePudding user response:
first open command console go to the root of your Laravel project and type:
php artisan make:command MyNewCommand // Choose name as you wish. in this example is MyNewCommand
New file created in app\Console\Commands. Open it and in function handle call your related controller and in the same time choose in name for your command:
protected $signature = 'auto:cron';
public function handle()
{
call('App\Http\Controllers\MyRelatedController@relatedFunction')->everyFiveMinutes();
}
Go to the app\Console and open Kernel.php add in the class Kernel this command:
protected $commands = [
Commands\MyNewCommand::class
];
in this file in function schedule add your related controller:
$schedule->command('auto:cron')
->everyFiveMinutes();
$schedule->call('App\Http\Controllers\MyRelatedController@relatedFunction')
->everyFiveMinutes();
At the end add your command to server cron job. Change this in order to your current PHP path and your Laravel project path:
/usr/local/bin/php /home/myaccount/public_html/laravel/artisan schedule:run >> /dev/null 2>&1
CodePudding user response:
After making the command and adding it to kernel, if it for testing, you can run "php artistan schedule:work"