laravel5.8消息通知浅析

本文介绍如何在Laravel框架中创建并使用通知系统,通过示例展示了如何为新注册的用户发送邮件及数据库通知。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

创建通知:php artisan make:notification InvoicePaid

这条命令会在 app/Notifications 目录下生成一个新的通知类。

<?php

namespace App\Notifications;

use Illuminate\Bus\Queueable;
use Illuminate\Notifications\Notification;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Notifications\Messages\MailMessage;

class InviocePaid extends Notification 
{
    use Queueable;

    /**
     * Create a new notification instance.
     *
     * @return void
     */
    public function __construct()
    {
        //
    }

    /**
     * Get the notification's delivery channels. 设置通知频道
     *
     * @param  mixed  $notifiable

        via方法接收一个 $notifiable 实例。这个实例将是通知实际发送到的类的实例。你可以用 $notifiable 来决定通知用哪些频道          来发送:
     * @return array
     */
    public function via($notifiable)
    {

     //database测试数据库频道,mail是测试邮箱频道
        return ['mail','database'];
    }

    /**
     * Get the mail representation of the notification.发送邮箱内容
     *
     * @param  mixed  $notifiable
     * @return \Illuminate\Notifications\Messages\MailMessage
     */
    public function toMail($notifiable)
    {
        return (new MailMessage)
                    ->line('The introduction to the notification.')
                    ->action('Notification Action', url('/'))
                    ->line('Thank you for using our application!');
    }

    /**
     * Get the array representation of the notification.
     *
     * @param  mixed  $notifiable
     * @return array
     */
    public function toArray($notifiable)
    {
        return [
            //
        ];
    }
    //插入数据库内容
    public function toDatabase()
    {
        return ['name'=>'xx'];
    }
}
具体看注释。

为了测试数据库频道,数据表新建通知表,存储所有的通知消息;由于创建用户user包含mail属性,邮箱频道无需操作。

# 创建表 php artisan notifications:table # 数据库迁移 php artisan migrate

 

这里就用系统默认的 App\User 模型使用这个 trait,它包含着一个可以用来发送通知的方法: notify。 notify方法需要一个通知实例做参数,于是就在注册控制器的新建用户方法中调用:

<?php

namespace App\Http\Controllers\Auth;

use App\User;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Validator;
use Illuminate\Foundation\Auth\RegistersUsers;
use App\Notifications\InvoicePaid;

class RegisterController extends Controller
{

    use RegistersUsers;

    /**
     * Where to redirect users after registration.
     *
     * @var string
     */
    protected $redirectTo = '/home';

    /**
     * Create a new controller instance.
     *
     * @return void
     */
    public function __construct()
    {
        $this->middleware('guest');
    }

    /**
     * Get a validator for an incoming registration request.
     *
     * @param  array  $data
     * @return \Illuminate\Contracts\Validation\Validator
     */
    protected function validator(array $data)
    {
        return Validator::make($data, [
            'name' => ['required', 'string', 'max:255'],
            'email' => ['required', 'string', 'email', 'max:255', 'unique:users'],
            'password' => ['required', 'string', 'min:8', 'confirmed'],
        ]);
    }

    /**
     * Create a new user instance after a valid registration.
     *
     * @param  array  $data
     * @return \App\User
     */
    protected function create(array $data)
    {
       $user = User::create([
            'name' => $data['name'],
            'email' => $data['email'],
            'password' => Hash::make($data['password']),
        ]);
       $user->notify(new InvoicePaid());//在这里调用
       return $user;
    }
}
以上即可,至于队列调用,再说。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值