How to create a cron in Laravel?

Asked

Viewed 13,616 times

4

I have to create a Cron on Laravel to send emails every 24 hours. I’m supposed to upload data from the bank. So after a query in the database cron should send the email with the query data.

  • One of the options would be to work with Task Scheduling. Documentation: https://laravel.com/docs/5.5/scheduling. Check this answer here at Stackoverflow: https://answall.com/questions/53587/automatizar-tarefas-comlaravel-5

1 answer

22


To create CRON in the follow the following steps:

First create a command as follows:

php artisan make:command ExampleCron --command=example:cron 

inside that file that was created in the folder app/Console/Commands/ExampleCron.php.

namespace App\Console\Commands;
use Illuminate\Console\Command;
use DB;
class ExampleCron extends Command
{
  
    protected $signature = 'example:cron';
  
    protected $description = 'Command E-mail';
  
    public function __construct()
    {
        parent::__construct();
    }
    // aqui você coloca a lógica do seu processo
    // pode utilizar todos os recursos do Laravel
    public function handle()
    {
        \DB::table('emails')->get(); // pega os e-mails
        // siga o código de sua preferencia
        // executando as funções de envio de e-mail
        $this->info('Example Cron comando rodando com êxito');
    }
}

Change the variables $signature and $description as an example and in the method handle() the logic of the process.

Second, to register this command enter folder and file app/Console/Kernel.php and open the Kernel.php to add to array of $commands add the command servant.

namespace App\Console;
use Illuminate\Console\Scheduling\Schedule;
use Illuminate\Foundation\Console\Kernel as ConsoleKernel;
class Kernel extends ConsoleKernel
{
    /**
     * The Artisan commands provided by your application.
     *
     * @var array
     */
    protected $commands = [
        Commands\Inspire::class,
        Commands\ExampleCron::class,
    ];
    /**
     * Define the application's command schedule.
     *
     * @param  \Illuminate\Console\Scheduling\Schedule  $schedule
     * @return void
     */
    protected function schedule(Schedule $schedule)
    {
        $schedule->command('inspire')->hourly();

        $schedule->command('example:cron')->daily(); // email diários
    }
}

Third, now add this line to your file cron

* * * * * php /path/to/artisan schedule:run 1>> /dev/null 2>&1

Can be configured in various ways and several hours, frame example:

Schedule Frequency Options

Method Description
->cron('* * * * * *'); Run the task on a custom Cron Schedule
->everyMinute(); Run the task Every minute
->everyFiveMinutes(); Run the task Every five minutes
->everyTenMinutes(); Run the task Every ten minutes
->everyFifteenMinutes(); Run the task Every Fifteen minutes
->everyThirtyMinutes(); Run the task Every Thirty minutes
->hourly(); Run the task Every hour
->hourlyAt(17); Run the task Every hour at 17 mins Past the hour
->daily(); Run the task Every day at Midnight
->dailyAt('13:00'); Run the task Every day at 13:00
->twiceDaily(1, 13); Run the task Daily at 1:00 & 13:00
->weekly(); Run the task Every week
->monthly(); Run the task Every Month
->monthlyOn(4, '15:00'); Run the task Every Month on the 4th at 15:00
->quarterly(); Run the task Every Quarter
->yearly(); Run the task Every year
->timezone('America/New_York'); Set the Timezone

Source: https://laravel.com/docs/5.5/scheduling#defining-schedules

Example: obtained and referenced in the link (source: http:/itsolutionstuff.com)

Your question has email, here on the site already has a minimum example, follows the same logic, follow this basic example that can help you understand the process.

References

Browser other questions tagged

You are not signed in. Login or sign up in order to post.