当我的数据透视表中的记录发生更改时,我想向我的标题(我认为这意味着所有 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/