ром Макаров
ром Макаров

Reputation: 165

Laravel get user phone from db

I got users table with phone row. I'm trying to send SMS then user is activated like that:

      public function activate($token)
    {
        User::whereActivationToken($token)->firstOrFail()->confirmEmail();
      $phone = $this->user->phone;

        flash()->success(trans('frontend.account.activated'));

        if (Auth::check()) {
            return redirect()->route('account.home');
        } else {
            return redirect()->route('auth.login');
Sms::send("activated!", function($sms) {
    $sms->to(['$phone']); # The numbers to send to.
});
        }
    }
}

But i'm getting error, Trying to get property of non-object. Whats wrong? Thanks for answers guys and please dont be mad, i'm new on laravel.

Upvotes: 2

Views: 925

Answers (2)

Alexey Mezenin
Alexey Mezenin

Reputation: 163938

Change your code to this:

$user = User::whereActivationToken($token)->firstOrFail();
$user->confirmEmail();
$phone = $user->phone;

Upvotes: 1

EddyTheDove
EddyTheDove

Reputation: 13259

Your error may be coming from this line

$phone = $this->user->phone;

$user exists nowhere in that function. Perhaps you defined it somewhere else. If that's the case, you can fix it with :

$user = User::whereActivationToken($token)->firstOrFail();
$phone = $user->phone;

Upvotes: 1

Related Questions