Laravel 8 使用 Notifiable Trait
通知可以通过两种方法发送: Notifiable trait 的 notify 方法或 Notification facade。首先,让我们来探索下使用 trait:
<?php
namespace App\Models;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
class User extends Authenticatable
{
use Notifiable;
} 这个 trait 由预设的 App\Models\User 模型使用,它含有一个可以用来发送通知的方法: notify。notify 方法需要接收一个通知实例参数:
use App\Notifications\InvoicePaid;
$user->notify(new InvoicePaid($invoice)); 提示: 请记住,你可以在任何模型中使用
Illuminate\Notifications\Notifiabletrait。而不仅仅是在User模型中。