简体   繁体   English

Auth::user()->id 返回“试图获取非对象的属性”——Laravel 5.8

[英]Auth::user()->id returns "Trying to get property of non-object" -- Laravel 5.8

UPDATED 2更新 2

I made like and unlike method.我做了喜欢和不喜欢的方法。 But when I try to like the article, it returns an error.但是当我尝试喜欢这篇文章时,它返回一个错误。 Route [login] not defined.

I am using passport API login.我正在使用护照 API 登录。 I am giving token for login etc... I login without a problem.我正在为登录等提供令牌......我登录没有问题。 I see the the pages only auth user can see.我看到只有 auth 用户才能看到的页面。 But it seems when I like the article, Auth:: doesn't understand user logged in or not.但似乎当我喜欢这篇文章时, Auth::不了解用户是否登录。 Maybe this is the problem.也许这就是问题所在。 Because I am using Passport?因为我使用的是 Passport? So in the controller instead of Auth::所以在控制器而不是Auth::

I used it like $user = $request->user();我用它就像$user = $request->user(); (you can see the controller below.) But still same error popping when I like the article. (你可以在下面看到控制器。)但是当我喜欢这篇文章时仍然出现同样的错误。 Route [login] not defined.

controller控制器

public function postLikeArticle( Request $request, $articleID )
{
    $article = Article::where('id', '=', $articleID)->first();
    $user = $request->user();

    $article->likes()->attach( $user->id, [
        'created_at'    => date('Y-m-d H:i:s'),
        'updated_at'    => date('Y-m-d H:i:s')
    ]);

  return response()->json( ['article_liked' => true], 201 );
}

public function deleteLikeArticle( Request $request, $articleID )
{
    $article = Article::where('id', '=', $articleID)->first();
    $user = $request->user();

    $article->likes()->detach( $user->id );

    return response(null, 204);
}

Routes路线

Route::middleware('auth:api')->group(function() {
    Route::get('/user', function (Request $request) {
        return $request->user();
    });

    Route::get('/articles/{id}/like', 'Api\ArticlesController@postLikeArticle');
    Route::get('/articles/{id}/like', 'Api\ArticlesController@deleteLikeArticle');

});

If you need to see anymore file.如果您需要查看更多文件。 please name it in the comment.请在评论中命名。

Your routes for like/dislike are not auth protected which means that the user might not be logged in, so you can group all routes requiring an authenticated user like this:您喜欢/不喜欢的路由不受auth保护,这意味着用户可能未登录,因此您可以将所有需要经过身份验证的用户的路由分组,如下所示:

// public routes out of the group, for example:

Auth::routes();

Route::middleware('auth:api')->group(function() {
    Route::get('/user', function (Request $request) {
        return $request->user();
    });

    Route::get('/articles/{id}/like', 'Api\ArticlesController@postLikeArticle');


    // you cannot have same routes for both, so this
    // Route::get('/articles/{id}/like', 'Api\ArticlesController@deleteLikeArticle');
    // should be this:
    Route::get('/articles/{id}/dislike', 'Api\ArticlesController@deleteLikeArticle');

});

Then either: Auth::id() or Auth::user()->id should give you the same.然后: Auth::id()Auth::user()->id应该给你相同的。 Or even the helper function so you don't need to worry for the imports..甚至是辅助函数,因此您无需担心导入..

auth()->id();

// or
auth()->user()->id;

If you need the user to be authenticated then you need to be using the middleware that ensures that.如果您需要对用户进行身份验证,那么您需要使用确保这一点的中间件。 In addition you need to also ensure that you are using the correct HTTP verbs depending on what you need to do, if anything your current definition is creating a conflict:此外,您还需要确保根据您需要执行的操作使用正确的 HTTP 动词,如果您当前的定义存在任何冲突:

Route::middleware('auth:api')->post('/articles/{id}/like', 'Api\ArticlesController@postLikeArticle');
Route::middleware('auth:api')->delete('/articles/{id}/like', 'Api\ArticlesController@deleteLikeArticle');

You also need to ensure your front-end is using the correct verbs as well.您还需要确保您的前端也使用正确的动词。

There are two reasons:有两个原因:

  1. If you don't use the middleware you're basically saying those routes don't require authentication如果您不使用中间件,您基本上是在说这些路由不需要身份验证
  2. If you're using the API guard to authenticate users then you need to explicitly use the auth:api middelware otherswise Laravel will attempt to authenticate the user using the default guard which is usually the session.如果您使用 API 保护来验证用户,那么您需要明确使用auth:api中间件,否则 Laravel 将尝试使用默认保护(通常是会话)来验证用户。 When using an API you should not use the session使用 API 时不应使用会话

instead of代替

Auth::user()->id

You can try this你可以试试这个

auth()->user()->id

you can refer laravel documentation for more information您可以参考laravel 文档以获取更多信息

Few simple points should resolve your confusion:几个简单的点可以解决您的困惑:

  1. "Trying to get property of a non object" means, Auth::user() is not an object which means it's null, you are trying to access a route publicly which needs an authenticated user which leads to our next point. “试图获取非对象的属性”意味着, Auth::user()不是一个对象,这意味着它是空的,您正在尝试公开访问需要经过身份验证的用户的路由,这会导致我们的下一点。
  2. Routes which use functions postLikeArticle() and deleteLikeArticle() should be inside the authorization middleware 'auth:api'使用函数postLikeArticle()deleteLikeArticle()路由应该在授权中间件'auth:api'
  3. You are using method get for both of your routes which use above functions, one should be using delete method.您正在为使用上述功能的两条路线使用get方法,其中一个应该使用delete方法。
  4. You can use Auth::id() as a substitute of Auth::user()->id just to be short hand.您可以使用Auth::id()作为Auth::user()->id的替代品,只是为了简写。

When using PASSPORT for authentication, you do not follow the usual Laravel way of user authorization hence Auth::user() is useless.使用 PASSPORT 进行身份验证时,您不遵循通常的 Laravel 用户授权方式,因此 Auth::user() 是无用的。 In order to fetch the authenticated user, you need to use:为了获取经过身份验证的用户,您需要使用:

$user = $request->user();

Where $request is an instance of Illuminate\\Http\\Request $requestIlluminate\\Http\\Request一个实例

Here is an example for you: Import the Request class in the beginning of the controller file:下面是一个示例:在控制器文件的开头导入 Request 类:

use Illuminate\\Http\\Request;

and your controller logic:和您的控制器逻辑:

public function postLikeArticle( Request $request, $articleID )
{
    $article = Article::where('id', '=', $articleID)->first();
    $user = $request->user();

    $article->likes()->attach( $user->id, [
        'created_at'    => date('Y-m-d H:i:s'),
        'updated_at'    => date('Y-m-d H:i:s')
    ]);

  return response()->json( ['article_liked' => true], 201 );
}

public function deleteLikeArticle( Request $request, $articleID )
{
    $article = Article::where('id', '=', $articleID)->first();
    $user = $request->user();

    $article->likes()->detach( $user->id );

    return response(null, 204);
} 

I hope it helps我希望它有帮助

声明:本站的技术帖子网页,遵循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 获取非对象的属性“id”; - Laravel: Trying to get property 'id' of non-object for Auth::User()->id; 在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.2 /试图获取非对象的属性 - Auth::user()->id wont work Laravel 5.2 / Trying to get property of non-object 试图获取非对象Laravel php 5.8的属性'cname' - Trying to get property 'cname' of non-object Laravel php 5.8 试图在laravel 5.8中获得非对象的属性 - Trying to get property of non-object in laravel 5.8 试图获取非对象 Laravel 5.8 错误的属性“title” - Trying to get property 'title' of non-object Laravel 5.8 Error 更新错误 - 尝试在 Laravel 5.8 中获取非对象的属性“id” - Update Error - Trying to get property 'id' of non-object in Laravel 5.8
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM