当用户通过电子邮件更新记录时,如何通过从指定的 table 中选择记录的 status_id 来通知所有管理员

How to notify all admins by selecting the status_id of the records from the specified table when the user updated the record via e-mail

我使用 Laravel 7 并且我有 topics table 我有 5 个状态这些状态 它们是来自状态 table.

的外键

主题Table

id topic owner_id status_id
1 A 1 2
2 B 2 6
3 C 3 2
4 D 4 6

状态Table

id name
1 Draft
2 Waiting for topic approval
3 Edit the topic
4 Do not approve the topic
5 Approved topic
6 Waiting for scoring
7 Approved score

我想通知所有管理员(用户 user_role=1 Table) 当用户通过电子邮件更新 status_id = 2 或 6 的记录时。

提前谢谢你。期待您的回复。

假设用户正在编辑 ID 为 1 的主题。

// import classes in your controller
use Illuminate\Support\Facades\Notification;
use App\Notifications\TopicUpdateNotification;
 
public function update(Request $request, $id)
{
    // some validation if needed

    $topic = Topic::find($id);
    $status = $topic->status_id;
    $topic->update([
        'topic' => $request->topic,
        // add any other column you want to update
    ]);
    
    // now we are checking if the topic status was 2 or 6
    if ($status == 2 || $status == 6) {
        // finding all the admins
        $admins = User::where('user_role', 1)->get();
        $user = auth()->user();
        // sending notification via Notification Facade
        Notification::send($admins, new TopicUpdateNotification($topic, $user));
    }
}

我们使用了一个名为 TopicUpdateNotification 的 class。这是一个通知 class,我们必须使用 artisan 命令创建它。

php artisan make:notification TopicUpdateNotification

您将在项目的 app/Notifications 目录中找到它。 class

的内容
<?php

namespace App\Notifications;

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

class TopicUpdateNotification extends Notification
{
    use Queueable;

    /**
     * Create a new notification instance.
     *
     * @return void
     */
    public function __construct($topic, $user)
    {
        $this->topic = $topic;
        $this->user = $user;
    }

    /**
     * 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)
        ->view(
            'topic_update_email', ['topic' => $this->topic, 'user' => $this->user]
        )
        ->from('support@yourcompany.com', 'Your Company Name') // you can omit this line if you have valid MAIL_FROM_ADDRESS and MAIL_FROM_NAME in your .env
        ->subject('Topic Updated');
    }

    /**
     * Get the array representation of the notification.
     *
     * @param  mixed  $notifiable
     * @return array
     */
    public function toArray($notifiable)
    {
        return [
            //
        ];
    }
}

最后在views文件夹下制作blade文件topic_update_email.blade.php

<!DOCTYPE html>
<html>

<head>
    <title></title>
</head
<body>
    <h1>User {{ $user->name }} updated the topic {{ $topic->id }}</h1>
</body>
</html>

您可以找到完整的 laravel 通知文档 here