Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

noblemfd's avatar

How to send notification with input from a form to multiple recipients

I have these two models in Laravel-5.8

UserNotification

class UserNotification extends Model
{
    protected $table = 'notifications';

    protected $primaryKey = 'id';

    protected $fillable = [
        'id',
        'sent_to',
        'sent_by',
        'subject',
        'message',
    ];

    public function sentTo()
    {
        return $this->belongsTo('App\Models\Hr\HrEmployee','sent_to');
    } 

    public function sentBy()
    {
        return $this->belongsTo('App\Models\Hr\HrEmployee','sent_by');
    }    
}

HrEmployee

class HrEmployee extends Model
{
    protected $fillable = [
              'id',
              'email',
              'first_name',
              'last_name',
        ];
}

Controller

public function createNotification()
{         
        $employees = HrEmployee::where('hr_status', 0)->get();     
    return view('create_notification', $employees);               
}


public function storeNotification(Request $request)
{     
    $request->validate([
      'sent_to' => 'required|array',
      'message' => 'required|string',
      'subject' => 'required|string',  
    ]);
    
        for($i=0; $i < count($request->recipients); $i++){
          $tb = new UserNotification;
          $tb->sent_by = Auth::user()->employee_id;
          $tb->sent_to = $request->recipients[$i];
          $tb->message = $request->msg;
          $n[] = $tb->attributesToArray();
        }
        UserNotification::insert($n);  
}

view blade

                @foreach($employees as $key => $employee)
                    <tr>
                            <td>
                                <div class="checkbox">
                                    <label>
                                        <input type="checkbox" name="recipients[]" form="msgForm"
                                            value="{{$employee->id}}">
                                    </label>
                                </div>
                            </td>                            
                        <td>
                            {{$employee->first_name}} {{$employee->last_name}}
                        </td>
                        <td>
                            {{$employee->email}}                             
                        </td>
                                         
                </tr>
                @endforeach 

   <form  action="{{route('storeNotification')}}" method="post" id="notification-form" class="form-horizontal" enctype="multipart/form-data">
       {{csrf_field()}}              
        <div class="card card-primary card-outline">
          <div class="card-body">
            <div class="form-group">
                <label class="control-label"> Subject:<span style="color:red;">*</span></label>
              <input type="text" class="form-control" name="subject"  placeholder="Subject:" value="{{old('subject')}}" style="width: 100%;"> 
            </div>
            <div class="form-group">
                <label class="control-label"> Write Message:<span style="color:red;">*</span></label>
                <textarea name="msg" class="form-control" id="compose-textarea" class="form-control" placeholder="Write Message Here ..." style="height: 300px" >  
                </textarea>
               
            </div>
          </div>
          <div class="card-footer">
            <div class="float-right">
              <button type="submit" class="btn btn-primary"><i class="far fa-envelope"></i> Send</button>
            </div>
          </div>
        </div>
       </form>

From the view, the users selects the employee he wants from the table and , types the message in the textarea, then clicks on send

In the Controller, I use UserNotification::insert($n); to insert into the database.

I want to send message into the email $employee->email

message content will be textarea name="msg" and subject input type="text" name="subject"

How to send notification with input from the form to the selected users

0 likes
1 reply

Please or to participate in this conversation.