【问题标题】:Laravel - LogicException: App\Models\User::token must return a relationship instance, but "null" was returnedLaravel - LogicException:App\Models\User::token 必须返回一个关系实例,但返回了“null”
【发布时间】:2021-08-03 06:54:29
【问题描述】:

在 Laravel-8 中,我正在为 ForgotPassword 创建一个端点,并将通知发送到用户电子邮件:

public function forgotPassword(ResetPasswordRequest $request)
{
try {
    $user = User::where('email', $request->email)->first();
    if (!$user){
        return $this->error('We can\'t find a user with that e-mail address.', 400);
    }
    $email  =   $request->email;
    $token  =   Str::random(10);

    $passwordReset = PasswordReset::updateOrCreate(
        ['email' => $user->email],
        [
            'email'         =>  $email,
            'token'         =>  $token,
            'created_at'    =>  now()->addHours(10),
         ]
    );

    // Send Email Based on condition
    if ($user && $passwordReset){
        $user->notify(
            new PasswordResetRequest($token)
        );
    }
    return $this->success('We have e-mailed your password reset link!', [
        'user'          => $user,
    ]);
    } catch(\Exception $e) {
        Log::error($e);
        return $this->error($e->getMessage(), $e->getCode());
    }
}

当我提交时,我收到了这个错误:

local.ERROR: LogicException: App\Models\User::token 必须返回一个关系实例,但返回的是“null”

当我删除时:

    if ($user && $passwordReset){
        $user->notify(
            new PasswordResetRequest($token)
        );
    }

通知:

class PasswordResetRequest extends Notification
{
    use Queueable;
    protected $token;
    public function __construct($token)
    {
        $this->token = $token;
    }

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

    public function toMail($notifiable)
    {
       $url = url('http://localhost:4200/reset-password-submit?token='.$this->token);
        return (new MailMessage)
           ->line('You are receiving this email because we        received a password reset request for your account.')
           ->action('Reset Password', url($url))
           ->line('If you did not request a password reset, no further action is required.');
    }
}

没有错误,它也被插入到password_resets表中。但我需要发送通知。

User 和 PasswordReset 模型甚至不相关。

我该如何解决这个问题?

谢谢

【问题讨论】:

  • @Professor - 是的。我用过 Laravel\Passport\HasApiTokens;并使用 HasApiTokens,
  • 文档使用App\Notifications\ResetPasswordNotification 而不是您正在使用的PasswordResetRequest,我猜这是自定义通知。里面有什么?
  • 您可以在 User 模型中使用 sendPasswordResetNotification 函数,如果您只是调度 PasswordReset 事件而不是从控制器发送通知:event(new PasswordReset($user));。不知道这是否能解决你的问题
  • 我已经用通知更新了我的代码。我有点困惑。正如您所建议的,我如何在模型和控制器中安排这些代码

标签: laravel


【解决方案1】:

似乎在通知中使用$this->token 时,您没有获得 PasswordResetRequest $token 属性:您获得的是用户护照令牌

尝试将令牌变量名称更改为其他名称。

class PasswordResetRequest extends Notification
{
    use Queueable;
    protected $url_token;
    public function __construct($url_token)
    {
        $this->url_token = $url_token;
    }

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

    public function toMail($notifiable)
    {
       $url = url('http://localhost:4200/reset-password-submit?token='.$this->url_token);
       return (new MailMessage)
           ->line('You are receiving this email because we received a password reset request for your account.')
           ->action('Reset Password', url($url))
           ->line('If you did not request a password reset, no further action is required.');
    }
}

【讨论】:

  • 谢谢@教授
猜你喜欢
  • 1970-01-01
  • 2019-11-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-05-23
  • 1970-01-01
  • 2023-02-04
  • 2017-04-22
相关资源
最近更新 更多