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_id
Possible or no value.
Then$user = User::find($activation->user_id);
When there is no value, The Returned isnull
And 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.