Rumah > Soal Jawab > teks badan
P粉7795658552023-09-05 11:13:17
Ini boleh berfungsi, tetapi lebih biasa untuk menulisnya seperti ini. Jika anda pasti pengguna anda telah log masuk, anda boleh melangkau sebarang semakan tambahan untuk $user.
<?php $user = Auth::guard('web')->user(); name: $user->name; mobile: $user->mobile; address: $user->address; city: $user->city; gender: $user->gender; ?>
Jika pengguna anda mungkin tidak log masuk, anda boleh menambah semakan khusus baris seperti ini:
<?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 ?? ''; ?>
Jika pengguna anda mungkin tidak log masuk dan anda ingin memastikan mereka sentiasa log masuk, anda boleh melakukan perkara berikut:
<?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; ?>