我在数据透视表上创建了一个通知系统,因此当我将用户分配给客户端时,它会发送通知,一切正常,但是通知没有存储用户和客户端的正确ID,所以现在我想从中获取我正在保存的控制器。这是我的代码: 用户模型:
public function clients(){
return $this->belongsToMany('App\Client','client_user');
}
public function sendClientAddedNotification($client)
{
$this->notify(new ClientAdded($client,$this));
}
客户端模型:
public function sellmanlist(){
return $this->belongsToMany('App\User' , 'client_user','client_id');
}
这是我分配给卖方的客户控制器,并将其输入到数据透视表中:
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]);
$user_notification = Auth::user();
$user_notification->sendClientAddedNotification($client->sellmanlist()->sync($sellman));
return view('admin.client.assign',compact('client_list','user'));
}
最后,这是我的通知,我想保存我在控制器中输入数据库的确切客户端和用户ID:
use Queueable;
protected $client;
protected $user;
/**
* Create a new notification instance.
*
* @return void
*/
public function __construct($client,$user)
{
$this->client = $client;
$this->user = $user;
}
public function via($notifiable)
{
return ['database'];
}
public function toArray($notifiable)
{
foreach ($this->user->clients as $client){
$user_assigned_id =$client->pivot->user_id;
$client_assigned_id =$client->pivot->client_id;
}
return [
'client_id' => $client_assigned_id,
'user_id' => $user_assigned_id,
'client_name' => 'ASD',
];
}
这里是通知表迁移
Schema::create('notifications', function (Blueprint $table) {
$table->uuid('id')->primary();
$table->string('type');
$table->morphs('notifiable');
$table->text('data');
$table->timestamp('read_at')->nullable();
$table->timestamps();
});
答案 0 :(得分:1)
在聊天讨论中,您似乎想要存储从请求中获得的client_id和user_id。试试这个
用户模型
public function clients(){
return $this->belongsToMany('App\Client','client_user');
}
public function sendClientAddedNotification($clientId, $userId)
{
$this->notify(new ClientAdded($clientId, $userId));
}
通知类
use Queueable;
protected $clientId;
protected $userId;
/**
* Create a new notification instance.
*
* @return void
*/
public function __construct($clientId,$userId)
{
$this->clientId = $clientId;
$this->userId = $userId;
}
public function via($notifiable)
{
return ['database'];
}
public function toArray($notifiable)
{
return [
'client_id' => $clientId,
'user_id' => $this->userId,
'client_name' => 'ASD',
];
}
控制器代码
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]);
$user_notification = Auth::user();
$client->sellmanlist()->sync($sellman);
$user_notification->sendClientAddedNotification($client_name, $sellman);
return view('admin.client.assign',compact('client_list','user'));
}
希望它会起作用