P粉7795658552023-09-05 11:13:17
这可以工作,但更常见的是这样写。如果您确定您的用户已登录,则可以跳过 $user 的任何额外检查。
<?php $user = Auth::guard('web')->user(); name: $user->name; mobile: $user->mobile; address: $user->address; city: $user->city; gender: $user->gender; ?>
如果您的用户可能未登录,您可以添加特定于行的检查,如下所示:
<?php $user = Auth::guard('web')->user(); name: $user->name ?? null; mobile: $user->mobile ?? null; address: $user->address ?? null; city: $user->city ?? null; gender: $user->gender ?? null; //OR, when outputting in html name: $user->name ?? ''; mobile: $user->mobile ?? '' address: $user->address ?? ''; city: $user->city ?? ''; gender: $user->gender ?? ''; ?>
如果您的用户可能未登录,而您想确保他们始终登录,您可以执行以下操作:
<?php $user = Auth::guard('web')->user(); if($user === null){ throw new AuthenticationException('User Not logged in'); } name: $user->name; mobile: $user->mobile; address: $user->address; city: $user->city; gender: $user->gender; //OR $user = Auth::guard('web')->user(); if($user === null){ abort(401) } name: $user->name; mobile: $user->mobile; address: $user->address; city: $user->city; gender: $user->gender; ?>