【问题标题】:Call to a member function notify() on null in Laravel 8在 Laravel 8 中调用 null 上的成员函数 notify()
【发布时间】:2021-03-20 02:45:42
【问题描述】:

我想向手机发送短信(如果他已经开启了双因素身份验证系统)。

所以在LoginController我添加了这个方法:

protected function authenticated(Request $request, $user)
{
    return $this->loggendin($request , $user);
}

这个loggendin 方法位于一个名为TwoFactorAuthentication 的特征中,如下所示:

trait TwoFactorAuthenticate
{
    public function loggendin(Request $request , $user)
    {
        if($user->hasTwoFactorAuthenticatedEnabled()) {
            auth()->logout();

            $request->session()->flash('auth' , [
                'user_id' => $user->id,
                'using_sms' => false,
                'remember' => $request->has('remember')
            ]);


            if($user->two_factor_type == 'sms') {
                $code = ActiveCode::generateCode($user);
                // Todo Send Sms
                $request->user()->notify(new ActiveCodeNotification($code , $user->phone_number));

                $request->session()->push('auth.using_sms' , true);
            }

            return redirect(route('twofa.token'));
        }

        return false;
    }
}

现在的问题是当我想登录时,屏幕上会出现这条消息:

错误在 null 时调用成员函数 notify()

指的是这一行:

$request->user()->notify(new ActiveCodeNotification($code , $user->phone_number));

这个ActiveCodeNotification 包含一些用于发送短信的设置。

如果您想参观,这里是:

class ActiveCodeNotification extends Notification
{
    use Queueable;

    public $code;

    public $phoneNumber;
    /**
     * Create a new notification instance.
     *
     * @return void
     */
    public function __construct($code , $phoneNumber)
    {
        $this->code = $code;
        $this->phoneNumber = $phoneNumber;
    }

    /**
     * Get the notification's delivery channels.
     *
     * @param  mixed  $notifiable
     * @return array
     */
    public function via($notifiable)
    {
        return [GhasedakChannel::class];
    }


    public function toGhasedakSms($notifiable)
    {
        return [
            'text' => "{$this->code}",
            'number' => $this->phoneNumber
        ];
    }
}

那么这里出了什么问题,我在 null 上调用了一个成员函数 notify(),而它的两个参数都有值。

所以如果你知道,请告诉我。我真的很感谢你们的任何想法......

谢谢。

【问题讨论】:

    标签: php laravel laravel-8


    【解决方案1】:

    试试这个:

    首先,确保您的 User 模型具有 Notifiable trait。

    用户模型类的顶部:

    use Illuminate\Notifications\Notifiable;
    

    之后:

    class User extends Model{
    use Notifiable; // ....
    

    然后……

    代替

    $request->user()->notify(new ActiveCodeNotification($code , $user->phone_number));
    

    使用这个

    $user->notify(new ActiveCodeNotification($code, $user->phone_number));
    

    或者

    调用auth()之前->logout();

    首先使用它:

    auth()->user()->notify(new ActiveCodeNotification($code, $user->phone_number));
    

    然后,你可以调用 auth()->logout();

    最近为我工作

    【讨论】:

      【解决方案2】:

      $request->user()LoginController 上此时将是 null,因为请求未经过身份验证且用户尚未在请求实例上设置。

      您必须改用 $user 参数:

      $user->notify(new ActiveCodeNotification($code , $user->phone_number));
      

      注意:确保您的 User 模型具有 Illuminate\Notifications\Notifiable 特征,以便能够使用 notify()

      【讨论】:

        猜你喜欢
        • 2021-07-20
        • 1970-01-01
        • 1970-01-01
        • 2021-09-24
        • 2022-01-08
        • 2021-05-04
        • 1970-01-01
        • 2021-03-30
        • 1970-01-01
        相关资源
        最近更新 更多