Laravel 5 Command Scheduler, How to Pass in Options

痴心易碎 提交于 2020-01-02 02:21:10

问题


I have a command that takes in a number of days as an option. I did not see anywhere in the scheduler docs how to pass in options. Is it possible to pass options in to the command scheduler?

Here is my command with a days option:

php artisan users:daysInactiveInvitation --days=30

Scheduled it would be:

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

Preferably I could pass in the option something along the lines of:

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

回答1:


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

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

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




回答2:


You could also try this as an alternative:

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!');

        });   
    }
}

And in your 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(); 

}


来源:https://stackoverflow.com/questions/30202268/laravel-5-command-scheduler-how-to-pass-in-options

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!