Laravel: Auth::user()->id trying to get a property of a non-object

后端 未结 18 1688
小鲜肉
小鲜肉 2020-12-04 11:42

I\'m getting the following error \"trying to get a property of a non-object\" when I submit a form to add a user, the error is apparently on the first line: Auth::user()->id

18条回答
  •  -上瘾入骨i
    2020-12-04 12:38

    Your question and code sample are a little vague, and I believe the other developers are focusing on the wrong thing. I am making an application in Laravel, have used online tutorials for creating a new user and authentication, and seemed to have noticed that when you create a new user in Laravel, no Auth object is created - which you use to get the (new) logged-in user's ID in the rest of the application. This is a problem, and I believe what you may be asking. I did this kind of cludgy hack in userController::store :

    $user->save();
    
    Session::flash('message','Successfully created user!');
    
    //KLUDGE!! rest of site depends on user id in auth object - need to force/create it here
    Auth::attempt(array('email' => Input::get('email'), 'password' => Input::get('password')), true);
    
    Redirect::to('users/' . Auth::user()->id);
    

    Shouldn't have to create and authenticate, but I didn't know what else to do.

提交回复
热议问题