這是代碼:
ActivationService.php
mailer = $mailer; $this->activationRepo = $activationRepo; } public function sendActivationMail($user) { if ($user->activated || !$this->shouldSend($user)) { return; } $token = $this->activationRepo->createActivation($user); $link = route('user.activate', $token); $message = sprintf('Activate account %s', $link, $link); $this->mailer->raw($message, function (Message $m) use ($user) { $m->to($user->email)->subject('Activation mail'); }); } public function activateUser($token) { $activation = $this->activationRepo->getActivationByToken($token); if ($activation === null) { return null; } $user = User::find($activation->user_id); //下面這行是53行. $user->activated = true; $user->save(); $this->activationRepo->deleteActivation($token); return $user; } private function shouldSend($user) { $activation = $this->activationRepo->getActivation($user); return $activation === null || strtotime($activation->created_at) + 60 * 60 * $this->resendAfter < time(); } }
錯誤資訊:
ErrorException in ActivationService.php line 53: Creating default object from empty value
53行的位置在代碼中有注釋,代碼是$user->activated = true;
問題:
上面的代碼如何解決這個錯誤?