phpjquerylaravelemailcheckbox

Laravel Sending email to all selected checkboxes


I am sending email to all members at once. Now i am trying to send emails to selected users. How can i do that?

This is my existing mailController

public function send_mail()
{
    $joinus = new Joinus;
    $mails = Joinus::all();
    $array = array();

    foreach ($mails as $mail)
    {
        array_push($array, $mail->email);

    };

    Mail::to($array)->send(new SendMail($joinus->email))->delay(60);

I am getting all users from Joinus model. Adding them to an array and sending emails to all arrays child. Am i need to add smt to SendMail? Or do i have to use jquery? If anyone give me code example i'll be gladful. This isn't to hard question. But i just didn't make it out.

Also this is the checkbox's place

@can('delete',app($dataType->model_name))
  <td>
    <input type="checkbox" name="row_id" id="checkbox_{{ $data->getKey() }}" value="{{ $data->getKey() }}">
   </td>
@endcan

Solution

  • Your send_mail() method should be changed to:

    public function send_mail(Request $request)
    {
        $mails = Joinus::whereIn('id', $request->input('row_id'))->pluck('email');
        Mail::to($mails)->send(new SendMail($joinus->email))->delay(60)
    }
    

    This will select all emails of the checked ids (from your frontend).

    Your input[type=checkbox]'s name attribute should be changed to row_id[], so it is an array and has multiple values (which are the ids of your records?).

    And set up Laravel to send emails according to their documentation for the driver you are intending to use: more info at the official documentation.