Laravel Notification不传递数据并收到错误“必须是App \\ Notifications \\ User”的实例

时间:2018-04-08 04:53:37

标签: laravel

我正在尝试使用Laravel Notification发送电子邮件但收到此错误

{
    "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
}

我的控制器功能

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);
}

我的通知文件

<?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('info@test.com')
            // ->name('Entrance India')
            ->subject('New OTP from SVS ')
            ->markdown('mail.userResetPassword', compact('user'));
    }

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

这是我的用户模型

<?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',
    ];
}

尝试使用Laravel Notification发送电子邮件但收到错误

但同样的事情是用户创建功能,但它不适用于重置密码功能

我哪里错了?

2 个答案:

答案 0 :(得分:1)

您需要添加 使用App \ User  在通知文件中。

答案 1 :(得分:1)

User班级

中加入Notification班级

<{em> <{em> <{em> }类。
您必须确保User可用 只需在__Cunstructor班级中使用此功能即可。

  

Notification