0
votes

I am trying to save my data against my userid but I am getting this error.

My code is:

        \App\Keys::create([
         $user = \Illuminate\Support\Facades\Auth::user(),
        'user_id' => $user,
        'accessTokenKey' => $accessTokenKey,
        'accessTokenSecret' => $accessTokenSecret
    ]);
    return 'You have successfully granted';

I am getting error user_id does not have a default value and when I passed Auth::user()->id it says trying to get properties of non-object id.

Any help would be appreciated!

1
you do not have an authenticated user1Leo
Auth::user()->id only gave you an id if any user logged in!Hiren Gohel
My user is logged in but it is not saving my user_id.syed1234
what should i do in this problemsyed1234
Why are you assigning the user within the create method?linktoahref

1 Answers

0
votes

That's because you do not have any authenticated user

if(Auth::check()){
        \App\Keys::create([
          $user = \Illuminate\Support\Facades\Auth::user(),
          'user_id' => Auth::id(),
          'accessTokenKey' => $accessTokenKey,
          'accessTokenSecret' => $accessTokenSecret
      ]);
}else{
    //logged in the user or
    return 'User is not logged in ';
}