[英]Laravel Notification only sends in one channel then does not detect the "toMail" method
我有一個名為 ActivityAdded 的通知 class,當它被調用時,它應該通知 2 個通道、數據庫和郵件
namespace App\Notifications;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Notifications\Messages\MailMessage;
use Illuminate\Notifications\Notification;
class ActivityAdded extends Notification implements ShouldQueue
{
use Queueable;
public $activity;
public function __construct($activity)
{
return $this->activity = $activity;
}
public function via($notifiable)
{
return ['database', 'mail'];
}
public function toArray($notifiable)
{
return [
'number' => $this->activity->project->id,
'by' => $this->activity->createdBy->name,
];
}
public function toMail($notifiable)
{
return (new MailMessage)->markdown('emails.activities.added',
[
'activity' => $this->activity
]
);
}
所以現在當我調用這個通知時,它成功地在通知表中添加了一個數據庫條目,但是在郵件部分出現錯誤,錯誤是
Call to undefined method App\Notifications\ActivityAdded::toMail()
我從望遠鏡的工作選項卡中得到錯誤。 我不知道發生了什么。
如果重要,這就是我調用通知的方式
$this->project->coordinatedBy->notify(new ActivityAdded($activity));
編輯:好的,這是調用它的整個 function
public function createActivity()
{
$validatedData = $this->validate();
$validatedData['created_by_user_id'] = auth()->id();
$validatedData['project_id'] = $this->project->id;
$activity = Activity::create($validatedData);
$this->project->createdBy->notify(new ActivityAdded($activity));
$this->project->coordinatedBy->notify(new ActivityAdded($activity));
$this->closeModalWithEvents([
$this->emit('refreshProjectComponent')
]);
}
另一個更新:它在生產服務器上工作得很好,只是在我的本地機器上失敗了。 所以肯定有其他問題。
我認為這應該進入 model:
use App\Notifications\ActivityAdded;
...
public function activityAdded($activity)
{
$this->notify(new ActivityAdded($activity));
}
然后使用 model 從您的文件中調用:
// get your model however you like:
$model = Model::find($id);
// then
$model->activityAdded();
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.