如何向Artisan处理命令传入数据 - Laravel 5.2

3

我希望你能帮我翻译一下如何将数据传递到我在Artisan命令中创建的handle方法中。

目前我的代码如下: (如果我在handle方法中使用DD打印出来的数据,它可以工作,所以它与Artisan命令链接)

控制器:

public function update(Request $request, $slug) {

        // Get the user assocated with Listing, and also get the slug of listing.
        $listing = $request->user()->listings()->where('slug', $slug)->first();

        // Flash success message after every update
        Message::set('Alright!', 'Your changes were saved successfully.');



        Artisan::call('emails:sendIfGuideUpdated');

// More code here...
}

我创建了一个工匠命令来处理发送电子邮件的功能:

class SendEmails extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'emails:sendIfGuideUpdated';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Send out an email every minute to users that have favored a guide when that Guide updates their Listing';

    public $listing;


    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct(Listing $listing)
    {
        $this->listing = $listing;

        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        // Get the data from the Guide Listing that was updated

        $name = $this->listing->name;
        dd($name);

        // Send an email to the Admin notifying that a comment was made under a blog post.

    }
}

这是我的内核文件,将处理邮件发送时的情况:

class Kernel extends ConsoleKernel
{
    /**
     * The Artisan commands provided by your application.
     *
     * @var array
     */
    protected $commands = [
         Commands\Inspire::class,
         Commands\SendEmails::class,
    ];

    /**
     * Define the application's command schedule.
     *
     * @param  \Illuminate\Console\Scheduling\Schedule  $schedule
     * @return void
     */
    protected function schedule(Schedule $schedule)
    {
        $schedule->command('emails:sendIfGuideUpdated')->everyMinute();
    }
}

我在将列表信息传递到处理方法中遇到了问题。正确的做法是什么?

1个回答

3
尝试像这样更新命令的签名:protected $signature = 'emails:sendIfGuideUpdated {listing}'; 不要填写__construct。然后你的处理方法看起来应该像这样:
public function handle()
{
    // Get the data from the Guide Listing that was updated
    $listing = $this->argument('listing')
    $name = $listing->name;
    dd($name);

    // Send an email to the Admin notifying that a comment was made under a blog post.

}

此命令的调用将为:
Artisan::call('emails:sendIfGuideUpdated', ['listing' => $listing]);

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接