首页 > 解决方案 > 通知门面发送静态方法捕获异常

问题描述

如何捕获 Notification::send 异常?此通知工作正常,但我想添加一些管理页面来查看状态。Notification::send 方法无论成功与否总是返回 null。现在完整代码:

namespace App\Listeners;

use App\Events\SendUserNotification;
use Illuminate\Support\Facades\Notification;
use App\Models\LogNotification;

class SendUserNotificationListener
{

public function handle(SendUserNotification $event)
{
    $users = $event->users;
    $notification = $event->notification;

    $logData = [];

    try {
        $send = Notification::send($users, $notification); // returned null
        $status = "success";

    } catch (\Exception $exception) {
        $status = "error";
    }

    foreach ($users as $user) {
        $logData[] = [
            "user_id" => $user->id,
            "type" => $notification->getType(),
            "status" => $status,
            "created_at" => now(),
            "updated_at" => now(),
        ];
    }

    LogNotification::insert($logData);
}

}

此代码无法在 Notification::send 方法中捕获任何异常。

标签: phplaravel

解决方案


推荐阅读