php - 当数据库数据透视表 laravel 5 中的记录发生更改时发送通知

标签 php laravel

当我的数据透视表中的记录发生更改时,我想向我的标题(我认为这意味着所有 View )通知部分发送一条消息,所以这是我的代码

这是客户端模型

public function sellmanlist() {
  return $this->belongsToMany('App\User' , 'client_user','client_id');
}

这是保存卖家列表的客户端 Controller

public function assignsellman(Client $client) {
  $user = User::all();
  $client_list = Client::all();
  return view('admin.client.assign',compact('client_list','user'));
}

public function assignsellmanSave(Request $request) {
  $user = User::all();
  $client_list = Client::all();
  $client = Client::with('sellmanlist')->firstOrFail();
  $sellman = $request->input('sellman');
  $client_name = $request->input('client');
  $client->sellmanlist()->attach($sellman,['client_id' =>$client_name]);
  return view('admin.client.assign',compact('client_list','user'));
}

现在我想向用户发送一条通知,表明在他的个人资料中已将客户分配给您,有任何线索如何执行此操作吗?

最佳答案

您需要创建通知类来通知用户添加的客户端

php artisan make:notification ClientAdded

之后编辑此文件,您可以在新文件夹 App\Notifications

中找到该文件
namespace App\Notifications;

use Illuminate\Notifications\Notification;

class ClientAdded extends Notification
{

    protected $client;

    public function __construct($client)
    {
        $this->client = $client;
    }

    public function via($notifiable)
    {
        return ['database']; //need to create notifications table check the below link
    }

    public function toArray($notifiable)
    {
       return [
              'client_id' => $this->client->id,
              'client_name' => $this->client->name,
          ];
    }

}

在您的 User 模型中添加以下代码,并确保您的 User 模型应使用 Notifying 特征

public function sendClientAddedNotification($client)
{
    $this->notify(new ClientAdded($client));
}

将这些类导入到User模型

use App\Notifications\ClientAdded;
use Illuminate\Notifications\Notifiable;

保存客户端后现在在 Controller 中

$user->sendClientAddedNotification($client); 

此处的$user应该是您要通知的用户

检查数据库通知https://laravel.com/docs/5.6/notifications#database-notifications

关于php - 当数据库数据透视表 laravel 5 中的记录发生更改时发送通知,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51400658/

相关文章:

php - 如果通过 For Looping 显示输入表单,如何多次插入 Laravel

php - 我想将背景图片放入 Laravel 元素的 Blade 中

php - 如何在 MySQL 中使用 [] 对 DATETIME 行进行排序

php - Laravel 4 分页方法

php - Laravel 更新数据库有 Eloquent 问题?

php - 如何在 Laravel 中动态更改 .env 文件中的变量?

image - Laravel 从 base64 返回图像预览

javascript - 如何以幻灯片形式显示 mysql 数据库中的图像

php - MySQL PHP : Can't get any results

php - 在 php 函数中满足条件时回显类中的 <svg> 元素