1

I am trying to use Laravel Notification to send email but getting this error

{
    "message": "Type error: Argument 1 passed to App\\Notifications\\UserResetPasswordNotify::__construct() must be an instance of App\\Notifications\\User, instance of Illuminate\\Database\\Eloquent\\Collection given, called in /home/fy3bgmgte060/public_html/svs.com/app/Http/Controllers/Api/LoginController.php on line 143",
    "status_code": 500
}

My Controller function

public function resendOTPTest(Request $request)
{
    $user = User::where(['mobile' => $request->mobile])->first();

    Notification::send($user, new UserResetPasswordNotify($user));   

    return response()->json(['message' => 'success','data' => 'OTP Sent', 'success' => true], 200);
}

my Notification file

<?php

namespace App\Notifications;

use Illuminate\Bus\Queueable;
use Illuminate\Notifications\Messages\MailMessage;
use Illuminate\Notifications\Notification;

class UserResetPasswordNotify extends Notification
{
    use Queueable;

    public $user;

    public function __construct(User $user)
    {
        $this->user = $user;
    }

    public function via($notifiable)
    {
        return ['mail'];
    }

    public function toMail($notifiable)
    {
        $user = $this->user;
        return (new MailMessage)
            ->from('[email protected]')
            // ->name('Entrance India')
            ->subject('New OTP from SVS ')
            ->markdown('mail.userResetPassword', compact('user'));
    }

    public function toArray($notifiable)
    {
        return [
            //
        ];
    }
}

this id my User Model

<?php

namespace App;

use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable;
use App\Order;

class User extends Authenticatable
{
    use Notifiable;

    protected $fillable = [
         'fname','lname', 'email','gender', 'password' 
    ];

    protected $hidden = [
        'password', 'remember_token',
    ];
}

while trying to use Laravel Notification to send email but getting above error

But same thing is working for User creation function but it is not working for reset Password function

Where am I wrong?

1
  • 1
    In your notification file add use App\User;. Commented Apr 8, 2018 at 9:20

3 Answers 3

1

Include User Class in your Notification Class

You are Injecting User Dependency as Typehint to the Magic Method __Cunstructor into your Notification Class.
You have to make sure Class is available there.
Simply use this in your Notification Class.

use App\User

Sign up to request clarification or add additional context in comments.

Comments

1

You need to add use App\User in Notification file.

Comments

0

Try using

$user->notify(new UserResetPasswordNotify($user))

1 Comment

Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.