dan3460's avatar

Database Update

I need to run a program that updates a database from a file that is uploaded to the server periodically. I'm using laravel scheduler to do this. I have already done the shell script that runs ftp to upload the file. Works wonderfully, much easier than fiddling with crontab. My question is with regards to the program that updates the database, before Laravel i would have create a php that created a new db instance and a series of sql commands to process the data. I'm guessing i could the same, but i was wondering if it was a more elegant solution to run a database updater in the scheduler?

Thanks for your help

0 likes
4 replies
Thyrosis's avatar

It depends on the data you are feeding Laravel from the file.

If each row stands for a model in your application, for instance a product with stock numbers, you could

  • loop through all lines
  • for each line, get the model from the data, based on productID
  • then save that model and move on to the next line.

That way your using already there models and eloquent to retrieve/save/update data, which beats the raw mysql code imho.

bobbybouwmann's avatar

Well you can simply call a command that is running database queries right?

Anyway, my suggestion would be to use migrations instead of files with queries. There is also a --path option in the php artisan migrate command which makes this possible for you. This way you can keep track in the database what's going on and you have some kind of history of performed queries ;)

Let me know if that works for you :D

1 like
dan3460's avatar

Thanks for the replays. I know how to do the work on the database, in the past i would have created a stand alone php file to do it and put it on the crontab. It feels to me that it is much more elegant to do it using the scheduler. Playing around and googeling i have created a command and I'm testing putting the code to update the database in the handle method of the command that i created. Intriguing the migration suggestion, are you suggesting having the logic for the update in a migration file and call this migration periodically?

dan3460's avatar

I case any one is following, i solve the problem in this way: I created a command, in my case UpdateDatabase, in the handle of this command I test if a file exist, if it does ti fires a method on the inventory model that updates the database, and then deletes the file that was checked at the beginning. The command is fire from the scheduler every 30 minutes.

Thanks for the help

Please or to participate in this conversation.