Laravel 5 Command Scheduler, How to Pass in Options
Asked Answered
W

2

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]);
Weirdie answered 12/5, 2015 at 22:7 Comment(0)
A
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

Adaadabel answered 12/5, 2015 at 22:17 Comment(5)
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.Tate
@Tate 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 runAdaadabel
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.Tate
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 laravel.com/docs/7.x/artisan#defining-input-expectationsAdaadabel
Thanks @Adaadabel it was a lack of use of protected $signature at fault.Tate
B
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 = '[email protected]';

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

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

}
Buffon answered 12/1, 2018 at 12:40 Comment(1)
What about options and not arguments, things like --dryRunVarien

© 2022 - 2024 — McMap. All rights reserved.