我需要每天向已订阅我的新闻通讯的用户发送电子邮件。
我是否通过创建job
例如
class ProcessEmailAlerts implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
public function __construct()
{
}
public function handle()
{
$alerts = App\Alert::all();
$articles = App\Article::latest()->orderBy('created_at')->get();
foreach($alerts as $alert) {
$alert->user->notify(new NewsLetterNotification($alert, $articles));
}
}
}
然后在App\Console\Kernel
中定义作业:
$schedule->job(new ProcessEmailAlerts)->daily();
还是应该创建如下命令:
class ProcessEmailAlerts extends Command
{
protected $signature = 'emailalerts:send';
protected $description = 'Send newsletter e-mails to subscribed users';
public function __construct()
{
}
public function handle()
{
$alerts = App\Alert::all();
$articles = App\Article::latest()->orderBy('created_at')->get();
foreach($alerts as $alert) {
$alert->user->notify(new NewsLetterNotification($alert, $articles));
}
}
}
然后在App\Console\Kernel
中定义作业:
$schedule->command('emailalerts:send')->daily();
基本上,我希望获得有关执行此类任务的正确方法的指南-使用作业进行调度还是使用命令进行调度?