Laravel 5.3 发送数据库通知



adminuser添加新约会时,应为所有admins以及分配的user创建数据库通知。查看通知时,所有admins都应看到所有通知,而用户应仅看到为其分配的通知。

public function submitAppointmentForm(Request $request){
$validator = Validator::make($request->all(), [
'respond' => 'required',
'user2_status' => 'required',
]);
if ($validator->fails()) {
return response()->json(['error'=>$validator->errors()->all()]);
}
else
{
$user = Auth::user();
$appointment = new Appointments();
$appointment->project_list_id = $request->project_id;
$appointment->respond = $request->respond;
$appointment->user2_status = $request->user2_status;
$appointment->date = $request->appointment_date;
$appointment->assigned_to = $request->assign_to;
$appointment->user2_note = $request->user2_note;
$appointment->assigned_by = $user->user_id;
$appointment->added_by = $user->user_id;
$appointment->save();
$assign_to = User::where('user_id', $request->assign_to)->first();
Notification::send($assign_to, new NewAppointmentNotification($request));
return response()->json(['success'=>'Successfully added']);
} 
}

上述代码通知仅针对分配的user添加。 不用于admins

如何在发送通知时添加管理员

Notification::send($assign_to, new NewAppointmentNotification($request));

更新:

多亏了Dees Oomens我让它工作,我根据我的要求做了一个小的修改

$assign_to = User::where('user_id', $request->assign_to)->first();
$users = User::whereHas('roles', function($q){
$q->where('name', 'admin');
})->get();
$users->push($assign_to);
Notification::send($users, new NewAppointmentNotification($request));

首先,您需要获得所有管理员。您正在使用委托,所以我不确定您如何使用什么角色名称,但我最好的猜测是:

$users = User::with(['roles' => function($query) {
$query->where('name', 'admin');
}])->where('id', '!=', $user->id)->get();
$users->push($assign_to);
Notification::send($users, new NewAppointmentNotification($request));

现在,$users数组中的所有用户都将收到通知。$users数组包含所有管理员(但不是当前经过身份验证的管理员(和$assign_to的用户。

最新更新