Laravel中的这段代码,我想使用它作为API, for each 访问我应用程序的人显示它.我想在他登录时获取用户ID
public function show($displayType)
{
try {
$data = houses::where('displayType', $displayType)->paginate(5);
$user = auth()->user(); // Get the authenticated user
$userId = $user ? $user->id : null; // Check if user is authenticated and get the user ID
return response()->json([
'status' => true,
'message' => 'You have successfully retrieved data',
'data' => $data,
'user_id' => $userId, // Include the user ID in the response
]);
} catch (\Throwable $th) {
return response()->json([
'status' => false,
'message' => $th->getMessage(),
], 500);
}
}
我想判断访问我网站的人是否已登录
但问题是当我发送令牌时返回用户id为空 这就是使用我的路由请求的方式. //这是我的路由,我在中间件判断中使用它
Route::get('houses/{displayType}',[housesController::class, 'show']);
Route::middleware('auth:api')->group(function () {})