Php 如何在laravel 5.6中从控制器向通知发送变量

Php 如何在laravel 5.6中从控制器向通知发送变量,php,laravel,notifications,relation,Php,Laravel,Notifications,Relation,我在pivot表上创建了一个通知系统,所以当我将用户分配给客户端时,它会发送一个通知,一切正常,但通知没有存储正确的用户和客户端id,所以现在我想从控制器获取它,我正在保存它。这是我的密码: 用户模型: public function clients(){ return $this->belongsToMany('App\Client','client_user'); } public function sendClientAddedNotification($client) {

我在pivot表上创建了一个通知系统,所以当我将用户分配给客户端时,它会发送一个通知,一切正常,但通知没有存储正确的用户和客户端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');
}
这是我分配给sellman使用并向pivot表输入数据的客户机控制器:

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'));
}
最后,这里是我的通知,我想保存我在controller中输入数据库的确切客户端和用户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();
    });

从聊天讨论中,您似乎想要存储从请求中获得的客户端id和用户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')); 
}

希望它能工作

从聊天讨论中,您似乎想存储从请求中获得的客户端id和用户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')); 
}

希望它能正常工作

我使用将其放入控制器并将其传递给
sendClientAddedNotification
然后
ClientAdded
通知,您想存储什么?您可以添加
通知
表结构吗,只需一个屏幕截图就可以了,这是它在sellman列表中附加sellman的语句,你为什么要储存它。最好存储
用户id
和客户端id。最好添加
通知
表结构。我想我之前已经回答过你了。我使用在控制器中获取它,并将它传递给
sendclienteddednotification
,然后
ClientAdded
通知,你想存储什么?你能添加你的
通知
表结构吗,只需一个屏幕截图就可以了,这是它附加到sellman列表中的语句,你为什么要储存它。最好存储
用户id
和客户端id。最好添加
通知
表结构。我想我在我们之前已经回答过你了。那正是我需要的谢谢@rkj你帮了我很多这正是我需要的谢谢@rkj你帮了我很多