There is a cron in laravel

1

I would like to know if laravel has a library to perform tasks (in my case sending emails) every N amount of time?

    
asked by Santiago Muñoz 13.12.2016 в 01:20
source

1 answer

4

If it exists, it is known as Task Scheduling , just add the line of the cron command as would normally be done:

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

As for how to schedule the tasks, it can be done in the kernel of the console, in the schedule method:

<?php

namespace App\Console;

use DB;
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 = [
        \App\Console\Commands\Inspire::class,
    ];

    /**
     * Define the application's command schedule.
     *
     * @param  \Illuminate\Console\Scheduling\Schedule  $schedule
     * @return void
     */
    protected function schedule(Schedule $schedule)
    {
        $schedule->call(function () {
            DB::table('recent_users')->delete();
        })->daily();
    }
}

Or you can schedule commands too:

$schedule->command('emails:send --force')->daily();

Among other options to launch the commands and their frequencies, which you can see in the documentation.

    
answered by 13.12.2016 / 02:22
source