Argument 3 passed to App\Events\NoticeAnnouncement::__construct() must be an instance of App\User

446 Views Asked by At

I am trying to send mail to all students when a notice will be posted by a teacher. For this, I am calling a event when the notice is being created, and passed notice,teacher and user through the event variable. But its giving a error saying that "Argument 3 passed to App\Events\NoticeAnnouncement::__construct() must be an instance of App\User, instance of Illuminate\Database\Eloquent\Collection given, called in C:\xampp\htdocs\iiucsmartclassroom\app\Http\Controllers\Teacher\NoticeController.php".

Here is my Notice Controller :

 public function submitNotice(Request $request){
    $validatedData = $request->validate([
    'notice_title' => 'required|max:255',
    'notice_description' => 'required|max:3000',
    'notice_file.*' => 'mimes:jpg,jpeg,pdf',
    ]);

    $data=array();
    $data['teacher_id']=$request->teacher_id;
    $data['notice_title']=$request->notice_title;
    $data['notice_description']=$request->notice_description;
    $data['notice_post_date']=$request->notice_post_date;
    $data['notice_post_time']=$request->notice_post_time;
    $image=$request->file('notice_file');

    if ($image) {
        $image_name= str::random(5);
        $ext=strtolower($image->getClientOriginalExtension());
        $image_full_name=$image_name.'.'.$ext;
        $upload_path='notices/';
        $image_url=$upload_path.$image_full_name;
        $success=$image->move($upload_path,$image_full_name);
        if ($success) {
            $data['notice_file']=$image_url;
            $notices = DB::table('notices')
                     ->insertGetId($data);

             $notice = Notice::find($notices);
             $teacher = Teacher::find($notice->teacher_id);
             $users = User::all();

             event(new NoticeAnnouncement($notice,$teacher,$users));
          if ($notices) {
             $notification=array(
             'message'=>'Notice Posted Successfully',
             'alert-type'=>'success'
              );
            return Redirect()->back()->with($notification);                      
         }else{
          $notification=array(
             'message'=>'Could not be able to post the Notice',
             'alert-type'=>'error'
              );
             return Redirect()->back()->with($notification);
         }      
            
        }else{

          return Redirect()->back();
            
        }
    }else{
          $notice = DB::table('notices')
                     ->insert($data);
          if ($notice) {
             $notification=array(
             'message'=>'Notice Posted Successfully',
             'alert-type'=>'success'
              );
            return Redirect()->back()->with($notification);                     
         }else{
          $notification=array(
             'message'=>'Could not be able to post the Notice',
             'alert-type'=>'error'
              );
             return Redirect()->back()->with($notification);
         }  
    }
}

Here is my Event :

public $notice,$teacher,$user;
/**
 * Create a new event instance.
 *
 * @return void
 */
public function __construct(Notice $notice,Teacher $teacher,User $user)
{
    $this->notice = $notice;
    $this->teacher = $teacher; 
    $this->user = $user;
}

Here is my Listener :

public function handle(NoticeAnnouncement $event)
{
        foreach ($event->user as $user) {
        Mail::to($user->email)->send(new SendNotice($event->notice,$event->teacher,$event->user));
    }
}
2

There are 2 best solutions below

4
On

You send a collection of users to the method instead of the user model:

Mail::to($user->email)->send(new SendNotice($event->notice,$event->teacher,$user));
0
On

You use find($key) function to get Illuminate\Database\Eloquent\Model from \Illuminate\Database\Eloquent\Collection.

public function handle(NoticeAnnouncement $event)
{
        foreach ($event->users as $key => $user) {
        Mail::to($user->email)->send(new SendNotice($event->notice,$event->teacher,$event->users->find($key)));
    }
}