gpt4 book ai didi

php - Auth::id() 返回 null laravel

转载 作者:搜寻专家 更新时间:2023-10-31 21:20:25 25 4
gpt4 key购买 nike

我有一个函数在哪里

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

当我添加 Auth::id() 时,即使我在我的 Controller 上声明了 auth facade,它也会返回 null

use Illuminate\Support\Facades\Auth;

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

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

最佳答案

auth:api 中间件中添加你的 auth protected routes

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

关于php - Auth::id() 返回 null laravel,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52055866/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com