简体   繁体   English

Laravel:试图为 Auth::User()->id 获取非对象的属性“id”;

[英]Laravel: Trying to get property 'id' of non-object for Auth::User()->id;

I have the following login controller:我有以下登录controller:

public function userLogin(Request $request) {

    $rules = [
        'email' => 'required|exists:users',
        'password'  => 'required'
    ];
    $request->validate($rules);
    $data = [
        'email' => $request->get('email'),
        'password'  =>  $request->get('password'),
    ];

    if(Auth::attempt($data))
    {
        $user = Auth::User();
        $userid = Auth::User()->id;
        
        return response()->json([
            'user'  =>  $user, 
            'token' =>  $user->createToken('yourAppName')->accessToken ,

             $userid
        ]);

    }
    else
    {
        return response()->json('Unauthorized', 401);
    }

}

My web.php ( Token is generated with Laravel Passport )我的web.php (令牌是用 Laravel 护照生成的)

Route::get('/customize/{id}', function ($id) {
 
    if (
        User::where('id', Auth::User()->id)->whereHas('profile', function ($query) use ($id) {
            return $query->where('id', $id);
        })
        ->exists()
    )  {
        return view( 'welcome' );
      }      
      return "no";
});

For some reason, when testing my logincontroller with POSTMAN , I actually receive a value for Auth::User()->id .出于某种原因,在使用logincontroller测试我的登录控制器时,我实际上收到了Auth::User()->id的值。 But in web.php I receive Trying to get property 'id' of non-object .但是在web.php我收到Trying to get property 'id' of non-object Any idea?任何想法?

here is a solution这是一个解决方案

you can find that user in db with email and password you have and get its id您可以使用 email 和您拥有的密码在 db 中找到该用户并获取其 ID

and then use LoginUsingId($id) to login your user然后使用 LoginUsingId($id) 登录您的用户

Find out why you are not considered as an authenticated user.找出为什么您被视为经过身份验证的用户。 I can guess you send Authentication token in your request headers when you use Postman.我猜你在使用 Postman 时会在请求标头中发送身份验证令牌。 that this is not possible when you hit the address in the browser directly.当您直接在浏览器中点击地址时,这是不可能的。

can u replace Auth::User()->id to Auth::user()->id maybe uppercase may be causing a problem您可以将Auth::User()->id替换为Auth::user()->id也许大写可能会导致问题

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Laravel:Auth::user()->id 试图获取非对象的属性 - Laravel: Auth::user()->id trying to get a property of a non-object 试图在控制器Laravel中获取非对象Auth :: user()-> id的属性“ id”? - Trying to get property 'id' of non-object Auth::user()->id in Controller Laravel? 在Laravel中使用Auth :: user()-> id摆脱“试图获取非对象的属性” - Get rid of “Trying to get property of non-object” with Auth::user()->id in Laravel 试图在laravel 5.4构造函数上获取非对象的属性Auth :: user()-> id - trying to get property of non-object on laravel 5.4 constructor Auth::user()->id Auth::user()->id 返回“试图获取非对象的属性”——Laravel 5.8 - Auth::user()->id returns "Trying to get property of non-object" -- Laravel 5.8 Auth :: user()-> id无法正常工作Laravel 5.2 /试图获取非对象的属性 - Auth::user()->id wont work Laravel 5.2 / Trying to get property of non-object “试图获取非对象的属性 'id'” Laravel - Auth::id() 命令 - “Trying to get property 'id' of non-object” Laravel - Auth::id() command LARAVEL 6.x = 试图获取非对象的属性“id” - LARAVEL 6.x = Trying to get property 'id' of non-object Laravel问题试图获取非对象的属性'id' - Laravel issue Trying to get property 'id' of non-object Laravel - 试图获取非对象的属性“id” - Laravel - Trying to get property 'id' of non-object
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM