In Laravel, you can use Blade templates to display content based on the user authentication status. The @auth
and @guest
directives in Blade make it easy to conditionally display content based on whether the user is authenticated or not.
@auth
<!-- Content to display for authenticated users -->
<p>Welcome, {{ auth()->user()->name }}!</p>
@endauth
Inside the @auth
directive, you can place HTML or any other Blade code to show content specifically for authenticated users. In the example above, the user's name is displayed using the auth()->user()->name
helper.
@guest
<!-- Content to display for guests -->
<p>Please log in to access this page.</p>
@endguest
Similarly, you can use the @guest
directive to conditionally show content only to non-authenticated users. In the example above, a login prompt message is displayed for guests.
@auth
<!-- Content for authenticated users -->
<p>Welcome, {{ auth()->user()->name }}!</p>
@else
<!-- Content for guests -->
<p>Please log in to access this page.</p>
@endauth
You can also combine the @auth
and @else
directives to show different content for authenticated and non-authenticated users.
Remember to add these Blade templates within your .blade.php
files, where you want to conditionally display the content based on user authentication status.