Laravel 5.3发送数据库通知
当Laravel 5.3发送数据库通知,laravel,notifications,laravel-5.3,Laravel,Notifications,Laravel 5.3,当管理员为用户添加新约会时,应为所有管理员以及分配的用户创建数据库通知。当查看通知时,所有的管理员都应该看到所有通知,而用户应该只看到分配给他们的通知 public function submitAppointmentForm(Request $request){ $validator = Validator::make($request->all(), [ 'respond' => 'required', 'user2_status' =&
管理员
为用户
添加新约会时,应为所有管理员
以及分配的用户
创建数据库通知。当查看通知时,所有的管理员都应该看到所有通知,而用户应该只看到分配给他们的通知
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']);
}
}
仅为已分配的用户添加了上述代码通知。不适用于管理员
如何在发送通知时也添加管理员
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\u to
如何识别管理员?users
表中是否有类似于Is\u admin
的属性,或者是通过关系完成的?我使用了委托来获得角色和权限。我可以得到管理员,但不知道如何通过发送($assign_to,$admin)感谢您的回复。我今天会检查这个,让您知道您的解决方案工作正常。我做了一个小的修改,只是按角色获取用户。谢谢你的帮助。干杯