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; ?>