Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel 5 Command Scheduler, How to Pass in Options

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]);
like image 397
zeros-and-ones Avatar asked May 12 '15 22:05

zeros-and-ones


2 Answers

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

}
like image 87
Bruce Tong Avatar answered Oct 24 '22 07:10

Bruce Tong


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

like image 34
Wader Avatar answered Oct 24 '22 09:10

Wader