Laravel5.3 error: 'creatingdefaultobjectfromemptyvalue'

Source: Internet
Author: User
This is the code: ActivationService. php {code ...} error message: {code ...} the position of the 53 lines is annotated in the code. the code is $ user-& amp; gt; activatedtrue; problem: how can the above code solve this error? This is the code:

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); // The following row contains 53 rows. $ 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 ();}}

Error message:

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

The position of the 53 lines contains comments in the code. the code is$user->activated = true;

Problem:
How does the above code solve this error?

Reply content:

This is the code:

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); // The following row contains 53 rows. $ 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 ();}}

Error message:

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

The position of the 53 lines contains comments in the code. the code is$user->activated = true;

Problem:
How does the above code solve this error?

Because$activation = $this->activationRepo->getActivationByToken($token);It is a non-null result you get through other methods.
$activation->user_idPossible or no value.
Then$user = User::find($activation->user_id);When there is no value, The Returned isnullAnd then you have 53 rows$user->activated = true;Then, an empty object is assigned... So .. The simplest way is to add

if(!$user){    return null;}

However, you can breakpoint a row to see what the returned result is and then find the problem.

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.