13

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]);
zeros-and-ones
  • 4,227
  • 6
  • 35
  • 54

2 Answers2

27

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

Wader
  • 9,427
  • 1
  • 34
  • 38
  • What about doing this via a shell command when you manually run the scheduler? Ie at a command line running "php artisan users:daysInactiveInvitation" would run the item normally but what would be the syntax to add a parameter to that ? I get [Symfony\Component\Console\Exception\RuntimeException] errors adding something on the end. – AdamJones Apr 30 '20 at 02:05
  • @AdamJones Can you give an example of what you're asking? The code in this answer allows you to add options and arguments to the artisan command the scheduler will run – Wader May 01 '20 at 08:26
  • Sure @Wader, I'm talking about running the command scheduler via the CLI, not via laravel code. As I mention above, using the example of the code discussed in this question, at a command prompt "php artisan users:daysInactiveInvitation" would run this schedule manually. I want to do that but pass a parameter to it like "php artisan users:daysInactiveInvitation ---days=30" but this doesnt work in the command line it seems. – AdamJones May 01 '20 at 12:05
  • Hmm, thats exactly how it should work. In your code are you trying to read that as an option or an argument? An option is given as a `--option`, arguments are space separated (e.g `php artisan my:command arg1 arg2 --option1=value --option2`). This doesn't actually have anything to do with the scheduler, more on artisan commands and inputs are available in the docs https://laravel.com/docs/7.x/artisan#defining-input-expectations – Wader May 01 '20 at 15:17
  • Thanks @Wader it was a lack of use of protected $signature at fault. – AdamJones May 11 '20 at 19:33
11

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(); 

}
Bruce Tong
  • 1,366
  • 15
  • 14