Laravel 5.3 Error: ' Creating default object from empty value '

Source: Internet
Author: User
Tags sprintf
This is the code:

activationservice.php

    
  Mailer = $mailer;        $this->activationrepo = $activationRepo; The Public Function Sendactivationmail ($user) {if ($user->activated | |! $this->shoul            Dsend ($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)->subje            CT (' Activation mail ');                }); The Public Function Activateuser ($token) {$activation = $this->activationrepo->getactiva                Tionbytoken ($token);            if ($activation = = = null) {return null;            } $user = User::find ($activation->user_id);            The following line is 53 rows.       $user->activated = true;         $user->save ();                $this->activationrepo->deleteactivation ($token);            return $user; } Private Function Shouldsend ($user) {$activation = $this->activationrepo->getactivati            On ($user); return $activation = = = NULL | |        Strtotime ($activation->created_at) + * $this->resendafter < time (); }        }

Error message:

    ErrorException in ActivationService.php line 53:    Creating default object from empty value

The position of the 53 line is commented in the code, and the code is$user->activated = true;

Problem:
How does the above code resolve this error?

Reply content:

This is the code:

activationservice.php

    
  Mailer = $mailer;        $this->activationrepo = $activationRepo; The Public Function Sendactivationmail ($user) {if ($user->activated | |! $this->shoul            Dsend ($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)->subje            CT (' Activation mail ');                }); The Public Function Activateuser ($token) {$activation = $this->activationrepo->getactiva                Tionbytoken ($token);            if ($activation = = = null) {return null;            } $user = User::find ($activation->user_id);            The following line is 53 rows.       $user->activated = true;         $user->save ();                $this->activationrepo->deleteactivation ($token);            return $user; } Private Function Shouldsend ($user) {$activation = $this->activationrepo->getactivati            On ($user); return $activation = = = NULL | |        Strtotime ($activation->created_at) + * $this->resendafter < time (); }        }

Error message:

    ErrorException in ActivationService.php line 53:    Creating default object from empty value

The position of the 53 line is commented in the code, and the code is$user->activated = true;

Problem:
How does the above code resolve this error?

Because $activation = $this->activationRepo->getActivationByToken($token); it's a non-null return you get through something else.
$activation->user_idMay or may not be a value.
Then $user = User::find($activation->user_id); there is no value, the return is null , and then you 53 lines $user->activated = true; to give an empty to refer to the object ... So.. The simplest you are is 52 lines below add

if(!$user){    return null;}

But this kind of thing you can break a line to see what goes back and find out

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.