Maison > développement back-end > tutoriel php > Laravel 5.3报错:`Creating default object from empty value`

Laravel 5.3报错:`Creating default object from empty value`

WBOY
Libérer: 2016-10-11 14:23:43
original
1605 Les gens l'ont consulté

这是代码:

ActivationService.php

<code>    <?php namespace App;
    
    
    use Illuminate\Mail\Mailer;
    use Illuminate\Mail\Message;
    
    class ActivationService
    {
    
        protected $mailer;
    
        protected $activationRepo;
    
        protected $resendAfter = 24;
    
        public function __construct(Mailer $mailer, ActivationRepository $activationRepo)
        {
            $this->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 <a href="%s">%s</a>', $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 </code>
Copier après la connexion
Copier après la connexion

错误信息:

<code>    ErrorException in ActivationService.php line 53:
    Creating default object from empty value</code>
Copier après la connexion
Copier après la connexion

53行的位置在代码中有注释,代码是$user->activated = true;

问题:
上面的代码如何解决这个错误?

回复内容:

这是代码:

ActivationService.php

<code>    <?php namespace App;
    
    
    use Illuminate\Mail\Mailer;
    use Illuminate\Mail\Message;
    
    class ActivationService
    {
    
        protected $mailer;
    
        protected $activationRepo;
    
        protected $resendAfter = 24;
    
        public function __construct(Mailer $mailer, ActivationRepository $activationRepo)
        {
            $this->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 <a href="%s">%s</a>', $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 </code>
Copier après la connexion
Copier après la connexion

错误信息:

<code>    ErrorException in ActivationService.php line 53:
    Creating default object from empty value</code>
Copier après la connexion
Copier après la connexion

53行的位置在代码中有注释,代码是$user->activated = true;

问题:
上面的代码如何解决这个错误?

因为$activation = $this->activationRepo->getActivationByToken($token);是你通过别的拿到的一个不为null的返回
$activation->user_id可能,也可能没值。
然后$user = User::find($activation->user_id);的时候没值,返回的是null,然后你53行$user->activated = true;就给一个空去指对象了。。。所以。。最简单你就是52行下面增加

<code>if(!$user){
    return null;
}</code>
Copier après la connexion

不过这类你可以断点一行行看返回是啥再去找问题

Étiquettes associées:
source:php.cn
Déclaration de ce site Web
Le contenu de cet article est volontairement contribué par les internautes et les droits d'auteur appartiennent à l'auteur original. Ce site n'assume aucune responsabilité légale correspondante. Si vous trouvez un contenu suspecté de plagiat ou de contrefaçon, veuillez contacter admin@php.cn
Tutoriels populaires
Plus>
Derniers téléchargements
Plus>
effets Web
Code source du site Web
Matériel du site Web
Modèle frontal