Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/laravel/10.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/linux/23.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 Auth::id()返回null laravel_Php_Laravel_Laravel 5 - Fatal编程技术网

Php Auth::id()返回null laravel

Php Auth::id()返回null laravel,php,laravel,laravel-5,Php,Laravel,Laravel 5,我有一个函数 public function index(){ $users = User::doesntHave('roles')->latest()->paginate()->except(Auth::id()); return UsersResource::collection($users); } 当我添加Auth::id()时,即使我在控制器上声明了Auth facade,它也会返回null use Illuminate\Support\Facades\

我有一个函数

public function index(){
   $users = User::doesntHave('roles')->latest()->paginate()->except(Auth::id());
   return UsersResource::collection($users);
}
当我添加
Auth::id()
时,即使我在控制器上声明了Auth facade,它也会返回null

use Illuminate\Support\Facades\Auth;
这是我的路线,它存储在
api.php

Route::resource('users','Users\AdminUsersController')->except([
    'create', 'edit'
]);

你已经登录了。听起来不像

不要使用Lightning\Support\Facades\Auth;相反,它只是使用Auth

您可以执行以下操作:

auth()->check(); // this checks to see if you're logged in

$userId = auth()->check() ? auth()->id() : null;

auth()->id(); // this is the same as Auth::id();

auth()->user();

auth:api
中间件中添加受身份验证保护的路由

Route::post('login','LoginController@login');

Route::middleware(['auth:api'])->group(function () {

    Route::resource('users','Users\AdminUsersController')->except([
    'create', 'edit'
    ]);

    //other authenticated Routes goes inside this block

}); 

对于Api身份验证,我建议您查看

$users=User::doesntHave('roles')->latest()->paginate()->除了(Auth::User()->id)

您正在使用哪个版本的laravel?检查此
Auth::user()->id
您的路由应该具有
Auth
中间件或控制器
u构造中的
$this->middleware('Auth')我稍后再试,先生。即使在api路由内,它也能工作吗?在api情况下,您必须使用
auth:api
“不要使用light\Support\Facades\auth;”为什么不能?先生,它说未经验证的状态401我需要安装passport吗?@初学者是的,需要安装laravel passport,然后需要生成accessToken,然后在标头中发送该令牌。你可以看这个,先生对不起,这是我第一次使用护照,我总是跳过这个,因为我不知道从哪里开始。例如,我有一个生成访问令牌的登录控制器。所以在我登录的过程中,我现在已经可以使用auth:api中间件并能够使用我自己的api了?localhost/app/public/api/users?不获取401状态?谢谢siryes,将您的登录控制器置于
auth:api
中间件路由块更新答案之外。登录并生成
accessToken
后,在
Authorization
标题中使用此accessToken,如
Authorization:Bearer accessToken
调用所有受保护的API。旁注:在laravel应用程序中使用虚拟主机是一种很好的做法。选中此处是,对于所有请求,您必须使用
accessToken
值附加
Authorization
头。是的,您可以将其存储在localstorage中。记住
$user->createToken('Token Name')->accessToken
生成一个个人令牌,并且它没有过期时间,因此您必须在注销api或类似的东西中撤销它。最好阅读答案链接中的文档