Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/230.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 在Laravel 8中对null调用成员函数notify()_Php_Laravel_Laravel 8 - Fatal编程技术网

Php 在Laravel 8中对null调用成员函数notify()

Php 在Laravel 8中对null调用成员函数notify(),php,laravel,laravel-8,Php,Laravel,Laravel 8,我想给手机发短信(如果他已经打开了双因素认证系统) 因此,在LoginController中,我添加了以下方法: protected function authenticated(Request $request, $user) { return $this->loggendin($request , $user); } 这个loggendin方法是一个名为TwoFactorAuthentication的特征的内部,它是这样的: trait TwoFactorAuthentica

我想给手机发短信(如果他已经打开了双因素认证系统)

因此,在
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;
    }
}
现在的问题是,当我想登录时,屏幕上会显示以下消息:

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

指的是这一行:

$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()

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

谢谢。

试试这个:

首先,确保您的用户模型具有应报告的特性

用户模型类的顶部:

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()之前->注销()

首先使用它:

auth()->user()->notify(new ActiveCodeNotification($code, $user->phone_number));
然后,您可以调用auth()->logout()

最近为我工作

由于请求未经身份验证且用户尚未在请求实例上设置,因此
$request->user()
LoginController
上的
null

您必须改用
$user
参数:

$user->notify(new ActiveCodeNotification($code , $user->phone_number));
注意:确保您的
用户
型号具有
照明\Notifications\Notifiable
特性,以便能够使用
通知()