仅在会议中注册用户的电子邮件时发送电子邮件通知

时间:2018-07-30 16:03:24

标签: php laravel

我有一个页面,用户可以在其中发送会议中注册用户的通知。在此页面中,用户可以选择想要向会议中注册的所有与会者发送通知。但是,用户还可以选择他要发送给会议中注册的特定用户的电子邮件。我有下面的代码来实现这一目标。

在特定会议中注册的所有参与者的通知都正常运行。

问题在于发送有关用户在“ participant_email”表单字段中引入的特定电子邮件的通知。问题是该电子邮件将发送到任何电子邮件,但通知应仅发送给在会议中注册的用户,否则应显示错误。

您知道如何实现吗?在下面的代码适用于所有参与者的情况下,即,如果用户发送了所有参与者的通知,并且没有注册任何参与者,则不会发送该通知,并且显示为“会议中没有任何参与者注册”。 。但是,如果用户正在发送特定电子邮件的通知,而不是会议中注册用户的电子邮件,该如何实现呢?

public function send(Request $request, $id){

    $conference = Conference::find($id);

    $message = $request->message;
    $subject = $request->subject;
     // if the user selected that wants to send email for a specific participant of the conference
    if($request->send_to == "participant"){
        $this->validate(request(), [
            'participant_email' => 'required|email|exists:users,email',                                             
        ]);

        Mail::to($request->participant_email)->send(new Notification($conference, $message, $subject));

        Session::flash('success', 'Notification sent.');

        return redirect()->back();
    }
    // if the user selected that wants to send an email for all participants of the conference
    if($request->send_to == "all"){

        $sendTo = User::whereHas('registrations', function ($query) use($id) {
            $query->where('conference_id', '=', $id);
        })->get();
    }else{
       $sendTo = User::whereHas('registrations.participants.registration_type', function ($query) use ($id, $request) {
              $query->where('id', '=', $request->send_to)
                 ->where('conference_id', '=', $id);
              })->whereHas('registrations', function ($query) use ($id) {
                  $query->where('conference_id', '=', $id);
              })->get(); 
    }

    foreach($sendTo as $user){
        $usersEmail[] = $user->email;
    }

    if(isset($usersEmail)) {
        foreach ($usersEmail as $userEmail) {
            Mail::to($userEmail)->send(new Notification($conference, $message, $subject));
        }
        Session::flash('success', 'Notification sent with success.');
        return redirect()->back();

    }
    else{
        Session::flash('no_participants', 'There are no participants registered in the conference.');
        return redirect()->back();
    }
}

更新的代码:

class NotificationController extends Controller
{
    public function index($id){

        $conference = Conference::find($id);
        $registrationType = RegistrationType::where('conference_id', $id)->get();


        return view('notifications.index')->with('conference', $conference)->with('registrationType', $registrationType);
    }

    public function send(Request $request, $id){


        $conference = Conference::find($id);


        $this->validate(request(), [
            'send_to' => 'required',
            'subject' => 'required',
            'message' => 'required' // The message field is required.
        ]);


        $message = $request->message;
        $subject = $request->subject;

        if($request->send_to == "participant"){
            $this->validate(request(), $this->participantRules($id));
            $emails[] = $request->participant_email;
        }

        else if($request->send_to == "all"){
            $emails = User::whereHas('registrations', function ($query) use($id) {
                $query->where('conference_id', '=', $id);
            })->pluck('email');
        }


        else{
            $emails = User::whereHas('registrations.participants.registration_type', function ($query) use ($id, $request) {
                $query->where('id', '=', $request->send_to)
                    ->where('conference_id', '=', $id);
            })->whereHas('registrations', function ($query) use ($id) {
                $query->where('conference_id', '=', $id);
            })->get(); // use pluck('email') instead of get to select only email

        }

        if(count($emails) > 0) {
            $this->sendNotification($emails, $conference, $request);
            Session::flash('success', 'Notificação enviada com sucesso.');
            return redirect()->back();
        }else{
            Session::flash('no_participants', 'There are no participants registered in the conference.');
            return redirect()->back();
        }
    }

    protected function participantRules($conferenceID){
        return [
            'email' => [
                'required',
                'email',
                Rule::exists('users')->where(function ($query) use ($conferenceID) {
                    $query->whereHas('registrations',  function ($query) use($conferenceID) {
                        $query->where('conference_id', '=', $conferenceID);
                    });
                }),
            ],
        ];
    }

    protected function sendNotification(array $emails, $conference, $request){
        foreach ($emails as $userEmail) {
            Mail::to($userEmail)->send(new Notification($conference, $request->message, $request->subject));
        }

        Session::flash('success', 'Notificaiton sent with success.');
        return redirect()->back();
    }

}

1 个答案:

答案 0 :(得分:0)

您可以像这样在exists验证中添加额外的过滤器

public function send(Request $request, $id){

    $conference = Conference::find($id);

    $message = $request->message;
    $subject = $request->subject;
    $emails = []; 

    if($request->send_to == "participant"){
        $this->validate(request(), $this->participantRules($id));

        $emails = User::whereHas('registrations', function ($query) use($id) {
                       $query->where('conference_id', '=', $id);
                    })->where('email', $request->participant_email)->pluck('email');

    }else if($request->send_to == "all"){
        $emails = User::whereHas('registrations', function ($query) use($id) {
            $query->where('conference_id', '=', $id);
        })->pluck('email');
    }else{
        $emails = User::whereHas('registrations.participants.registration_type', function ($query) use ($id, $request) {
            $query->where('id', '=', $request->send_to)
                  ->where('conference_id', '=', $id);
            })->whereHas('registrations', function ($query) use ($id) {
               $query->where('conference_id', '=', $id);
            })->pluck('email'); 

    }

    if(count($emails) > 0) {
        $this->sendNotification($emails, $conference, $request);
        Session::flash('success', 'Notification sent with success.');
        return redirect()->back();
    }else{
        Session::flash('no_participants', 'There are no participants registered in the conference.');
        return redirect()->back();
    }
}

验证规则

protected function participantRules($conferenceId){
    return [
        'participant_email' => 'required|email'
    ];
}

发送通知

protected function sendNotification($emails, $conference, $request){
    foreach ($emails as $userEmail) {
        Mail::to($userEmail)->send(new Notification($conference, $request->message, $request->subject));
    }
}