Is it appropriate to display all user information using auth::guard laravel authentication?
P粉256487077
P粉256487077 2023-09-04 17:46:44
0
1
476
<p>I am new to Laravel. I have a question in my mind... is it correct to use auth::guard to display all user information. Am I using Laravel Breeze authentication? </p> <p>For example. Is that so-</p> <pre class="brush:php;toolbar:false;">name: Auth::guard('web')->user()->name mobile: Auth::guard('web')->user()->mobile address: Auth::guard('web')->user()->address city: Auth::guard('web')->user()->city gender: Auth::guard('web')->user()->gender</pre> <p>Like this? </p>
P粉256487077
P粉256487077

reply all(1)
P粉779565855

This can work, but it's more common to write it like this. If you're sure your user is logged in, you can skip any extra checks for $user.

<?php
$user = Auth::guard('web')->user();
name: $user->name;
mobile: $user->mobile;
address: $user->address;
city: $user->city;
gender: $user->gender;
?>

If your user may not be logged in, you can add a row-specific check like this:

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

If your users may not be logged in and you want to make sure they are always logged in, you can do the following:

<?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;
?>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template