gpt4 book ai didi

laravel - 如何将数据传入 Artisan handle 命令 - Laravel 5.2

转载 作者:行者123 更新时间:2023-12-05 04:13:03 24 4
gpt4 key购买 nike

我会将数据传递到我在 Artisan Command 中创建的句柄方法吗?

这是我目前所拥有的:(如果我在 handle 方法中添加一些东西,它会起作用,所以它与 Artisan Command 的链接)

Controller :

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...
}

我用来处理发送电子邮件的 Artisan Command:

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

我无法将列表信息传递到处理方法中。这样做的正确方法是什么?

最佳答案

尝试像这样更新命令的签名:protected $signature = 'emails:sendIfGuideUpdated {listing}';

并将 __construct 留空。

那么您的 handel 方法将如下所示:

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

关于laravel - 如何将数据传入 Artisan handle 命令 - Laravel 5.2,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38617111/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com