Laravel 4 and crontab

In many projects, you need use crontab (cron jobs) to execute some tasks as sending email or delete waste record in DB. With Laravel Project, you can do this easier.

Be strong enough to let go and patient enough to wait for what you deserve

Be strong enough to let go and patient enough to wait for what you deserve

Create a command in Laravel 4:

With command above, Laravel will create a file named FirstCommand.php in directory  app/commands/

FirstCommand.php contains source code as below:

You should change this line:

E.g:  I changed to

Laravel Command supports you use parameters in your command. To add or remove parameters, you should look at getArguments and getOptions functions

And the most important function is fire() function. The command will execute source code that is wrote in that function.

I did the most simple changed to show you the easiest to understand Laravel Command. Please see file FistCommand.php after changing:

Next step, you need to register the command with Laravel CLI. So easy, you open app/start/artisan.php file, and add one line as below:

You are done creating Laravel Command. To test, you could use command below:

The output above mean you successfully register a command.

Finally, put your command into the crontab:

Add line (run command every 2 minutes):

That’s all. Thank you for talking time to read this.

 

 

Laravel 4.2 Setup Working Environment

Laravel 4.2 setup working environment is an important task should do at the beginning a project. Config files, Git, Gitignore is things you should take care. The below will show you the way to store config files to separate working environment as development, staging and production.

A man who conquers himself is greater than one who conquers a thousand men in battle

A man who conquers himself is greater than one who conquers a thousand men in battle

Detect Working Environment boostrap/start.php:

For security reason, from Laravel 4.1,  detectEnvironment function only supports hostname.

Create directories in app/config:

Push config files (e.g database.php, app.php …) to directories that you pretend use for each environment, it will be override the default config value.

Laravel Multiple Database Connection

Laravel Multiple Database Connection – this is way to setup and work with multiple database connection in Laravel

The soul always knows what to do to heal itself. The challenge is to silence the mind

The soul always knows what to do to heal itself. The challenge is to silence the mind

Define connections in config file app/config/database.php

Use the second DB in Eloquent Model

Use the second DB Connection in Controller

Query Builder