我想安排一项任务,该任务将在满足特定条件时向已预订特定产品的选定用户发送电子邮件通知。这是任务的设置方式
$schedule->call(function () {
// get reservation collection
$reservations = Reservation::all();
// iterate over reservation collection
foreach ($reservations as $reservation) {
// get difference in hours between now and product start date
$timeDiff = Carbon::parse($reservation->product->start)->diffInHours();
// send mail notification to reservation owners
if ($timeDiff > 2) {
// get users who reserved the product
$users = Reservation::where('product_id', $reservation->product_id)->pluck($reservation->user->username);
//notify user
Notification::send($users, new ReservedProductPurchase($reservation));
}
}
})->everyMinute();
当我运行命令时php artisan schedule:run,它抛出一个错误
SQLSTATE[42S22]:未找到列:1054 未知列
“字段列表”中的“mymail@domain.com ”(SQL:选择mymail@domain. comfrom reservationswhere product_id= 2)
当然,我没有在我的预订表中保存电子邮件(在这种情况下为用户名),这就是发生错误的原因。
用户和预约的关系是One To Many用户hasMany预约和用户预约belongsTo。
我应该如何检索我希望将通知发送到的电子邮件(用户名)集合?
米脂
斯蒂芬大帝
回首忆惘然