0

The code below has an error. I am using Laravel 5.3 and php 7.0.

I google it but still not clear, any help would be greatly appreciated.

ActivationService.php

<?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);
        //Below is line 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();
    }

}

Error message

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

Line 53 of the code above is like this:

$user->activated = true;

How could I settle the issue?

1
  • 2
    $user = User::find($activation->user_id); returns false or null, probably. Have you tried dumping $activation->user_id? Why don't you use Auth::user()->id instead? Commented Oct 10, 2016 at 19:50

1 Answer 1

2

The problem is most likely because $user = User::find($activation->user_id); returns null or false.

When you end up in these kinds of situations always try dumping the variable where the problem is occurring, in this case dd($activation->user_id)

I would suggest using Auth::user()->id instead.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.