美文网首页laravel
laravel6.X通知消息(notifications)类的教

laravel6.X通知消息(notifications)类的教

作者: 前端组件库 | 来源:发表于2020-02-12 15:26 被阅读0次

利用laravel的notification类可以简便地给每个用户发自定义消息,通过邮件,站内信(数据库)或者手机短消息等渠道。我们在此探索下数据库和邮件的消息通知类。在laravel里面,每个通知就是一个类。

1.通过数据库通知

创建通知类数据表

php artisan notifications:table

这个命令会创建一个通知表,表名是notifications,每个发出去的通知都放在里面。

然后创建一个通知类,我们在此谋划发通知的逻辑:

php artisan make:notification RegisterNotify

这个命令会创建一个通知类,在App/Notifications这个文件夹下面, 创建一个RegisterNotify.php这个文件,让我们来看看这个文件:

<?php

namespace App\Notifications;

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

class RegisterNotify extends Notification
{
    use Queueable;

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

    /**
     * Get the notification's delivery channels.
     *
     * @param  mixed  $notifiable
     * @return array
     */
    public function via($notifiable)
    {
        return ['mail'];
    }

    /**
     * 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 [
            //
        ];
    }
}

我们可以看到里面有构造方法,通过这个构造方法,我们可以引入需要的一些变量。

via方法定义了通知的渠道,默认里面已经有了一个渠道,通过邮件,我们可以添加一个渠道,例如通过数据库,这样我们可以改写为:

return ['mail', 'database'];

toMail方法里我们已经有了发送的内容模板,我们可以插入变量。
如果我们想通过数据库,那么我们就要加一个方法:toDatabase()

我们可以这样写:

public function toDatabase()
    {

        return [
           'title' => "欢迎注册成为用户!",
           'description' => "请您注意在此网站的言行!"
        ];
    }

这个会返回通知内容的数组,并存到通知类的表notifications的data字段里。

为某个用户发站内信

获得当前用户的实例并发送通知:

$user = User::findOrFail(auth()->id());
$user->notify(new RegisterNotify());

获取当前用户的所有通知并分页:

$notifications = Auth::user()->notifications()->paginate(20);
//或者
$user = User::findOrFail(auth()->id());
$notifications = $user->notifications()->paginate(20);

获取当前用户未读消息的数量

auth()->user()->unreadNotifications()->count();

标记消息为已读

public function show($id)
    {
        $notification = auth()->user()->notifications()->where('id', $id)->first();

        if ($notification) {
            $notification->markAsRead();
            return redirect()->back();
        }
    }

为每个用户发通知

$users = Users::all();
Notification::send($users, new RegisterNotify());

在视图种分享您的数据(data字段), 例如:

               @foreach($notifications as $notification)
                    <div class="card mb-3">
                        <div class="card-header">{{$notification->data['title']}}</div>
                                <div class="card-body">
                                    <div class="card-text">
                                        {{$notification->data['description']}}
                                    </div>
                                </div>
                    </div>
                @endforeach

相关文章

网友评论

    本文标题:laravel6.X通知消息(notifications)类的教

    本文链接:https://www.haomeiwen.com/subject/dshcfhtx.html