Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/254.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php Laravel:获取已登录用户的id_Php_Laravel - Fatal编程技术网

Php Laravel:获取已登录用户的id

Php Laravel:获取已登录用户的id,php,laravel,Php,Laravel,我正在使用Laravel Passport来管理用户的身份验证。 当用户登录时,在浏览器的localstorage中,我有以下信息: { "user": { "id": 4, "name": "test", "email": "test@gmail.com",

我正在使用Laravel Passport来管理用户的身份验证。 当用户登录时,在浏览器的localstorage中,我有以下信息:

    {
        "user": {
            "id": 4,
            "name": "test",
            "email": "test@gmail.com",
            "email_verified_at": null,
            "created_at": "2021-05-08T06:57:27.000000Z",
            "updated_at": "2021-05-08T06:57:27.000000Z"
        },
        "token": "eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiIsImp0aSI6Ijk2MjkxN2YyYzAwZjM5NmMwMTAzYzRjYTYwODk1YmYxMzY1MDE5OGQ1ZDQ5ZDJjNThlYzc0YmRkMzg2ZTk3MTE3MWE3ZWRjNjMyNjkwNjlhIn0.eyJhdWQiOiIxIiwianRpIjoiOTYyOTE3ZjJjMDBmMzk2YzAxMDNjNGNhNjA4OTViZjEzNjUwMTk4ZDVkNDlkMmM1OGVjNzRiZGQzODZlOTcxMTcxYTdlZGM2MzI2OTA2OWEiLCJpYXQiOjE2MjA0Njg0MjQsIm5iZiI6MTYyMDQ2ODQyNCwiZXhwIj"
}
My
web.php

use Illuminate\Support\Facades\Route;
use App\Models\Profile;
use App\Models\User;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;

Auth::routes();

Route::get('/customize/{id}', function ($id) {
   
    if (
        User::where('id', Auth::id())->whereHas('profile', function ($query) use ($id) {
            return $query->where('id', $id);
        })
        ->exists()
    )  {
        return true;
      }      
      return false;
});
如果我将
Auth::id()

如何获取当前登录用户的id?

编辑:到目前为止我尝试的内容:

auth()->guard('api')->user()
Auth::id()
Auth::user()->id

我相信您可能只需要引用
Auth::user()->id
您现在拥有的
Auth::id()


我正在获取
尝试获取非对象的属性“id”
@AlyaKra
Auth::user()
在本例中返回一个空对象。你在使用哨兵吗?如果是这样,请尝试
Sentry::getUser()->id
我明白了。不,我不认为我在使用
哨兵
。我只用拉威尔护照作为代币。
use Illuminate\Support\Facades\Route;
use App\Models\Profile;
use App\Models\User;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;

Auth::routes();

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 true;
      }      
      return false;
});