Auth :: id()返回null laravel

问题描述:

我有一个功能,其中

public function index(){
   $users = User::doesntHave('roles')->latest()->paginate()->except(Auth::id());
   return UsersResource::collection($users);
}

当我在Auth::id()处添加dd时,即使我在控制器上声明了auth Facade,它也会返回null

when i dd the Auth::id() it returns null even if I declared the auth facade on my controller

use Illuminate\Support\Facades\Auth;

这是我的路线,存储在api.php

this is my route which is stored inside api.php

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

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身份验证,我建议您查看 https://laravel.com/docs/5.6/passport

For Api authentication i suggest you to look https://laravel.com/docs/5.6/passport