Laravel 5命令调度程序,如何传入选项

时间:2015-05-12 22:07:54

标签: laravel command queue scheduled-tasks laravel-5

我有一个作为选项需要几天的命令。我没有在调度程序文档中看到如何传入选项。是否可以将选项传递给命令调度程序?

这是带有日期选项的命令:

php artisan users:daysInactiveInvitation --days=30

预定会是:

 $schedule->command('users:daysInactiveInvitation')->daily();

最好我可以通过以下方式传递选项:

 $schedule->command('users:daysInactiveInvitation')->daily()->options(['days'=>30]);

2 个答案:

答案 0 :(得分:16)

You can just supply them in the Democrats.class function. The string given is literally just run through artisan as you would normally run a command in the terminal yourself.

command()

See https://github.com/laravel/framework/blob/5.0/src/Illuminate/Console/Scheduling/Schedule.php#L36

答案 1 :(得分:3)

你也可以尝试这个替代方案:

namespace App\Console\Commands;

use Illuminate\Console\Command;

use Mail;

class WeeklySchemeofWorkSender extends Command
{
    protected $signature = 'WeeklySchemeofWorkSender:sender {email} {name}';

public function handle()
{
    $email = $this->argument('email');
    $name = $this->argument('name');

    Mail::send([],[],function($message) use($email,$name) {

    $message->to($email)->subject('You have a reminder')->setBody('hi ' . $name . ', Remember to submit your work my friend!');

        });   
    }
}

在你的Kernel.php中

protected function schedule(Schedule $schedule)
{

  /** Run a loop here to retrieve values for name and email **/

  $name = 'Dio';
  $email = 'dio@theworld.com';

  /** pass the variables as an array **/

  $schedule->command('WeeklySchemeofWorkSender:sender',[$email,$name])
->everyMinute(); 

}