阅读(1101) (8)

Laravel 8 使用 Notifiable Trait

2021-07-06 09:24:39 更新

通知可以通过两种方法发送: Notifiable trait 的 notify 方法或 Notification facade。首先,让我们来探索下使用 trait:

<?php

namespace AppModels;

use IlluminateFoundationAuthUser as Authenticatable;
use IlluminateNotificationsNotifiable;

class User extends Authenticatable
{
    use Notifiable;
} 

这个 trait 由预设的 AppModelsUser 模型使用,它含有一个可以用来发送通知的方法: notifynotify 方法需要接收一个通知实例参数:

use AppNotificationsInvoicePaid;

$user->notify(new InvoicePaid($invoice)); 

提示: 请记住,你可以在任何模型中使用 IlluminateNotificationsNotifiable trait。而不仅仅是在 User 模型中。