Auth :: id()返回null laravel [英] Auth::id() returns null laravel

查看:258
本文介绍了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

这篇关于Auth :: id()返回null laravel的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆