John Davidson

php - Is there a way of getting authenticated user ID and use it on navigation.blade?

0 comments
Message:


I'm building a new Laravel app, using Breeze.
What i'm trying to do is get the authenticated user id to redirect it to the profile route, which is:


Route::group([
'prefix' => 'profile',
'as' => 'profile.',
'middleware' => ['auth']
], function () {
Route::get('/{id}', [ProfileController::class, 'show'])->name('profile');
}
);

But, on the layout of navigation, as shown below, i can't manage to get it on the :href.
I already tried some approaches, like:


:href="{{ route('profile', [Auth::user()->id]) }}"
:href="{{ route('profile',/([Auth::user()->id])) }}"

But none of them seems to work.


The navigation.blade.php dropdown part:


   <x-slot name="content">
<x-dropdown-link :href="route('admin.aprovar')">
{{ __('Aprovações') }}
</x-dropdown-link>
<x-dropdown-link>
{{ __('Perfil') }}
</x-dropdown-link>
<!-- Authentication -->
<form method="POST" action="{{ route('logout') }}">
@csrf

<x-dropdown-link :href="route('logout')"
onclick="event.preventDefault();
this.closest('form').submit();">
{{ __('Log Out') }}
</x-dropdown-link>
</form>
</x-slot>

Any help or hint would be appreciated.
Thanks for your time!



My Answer:
> Yes, you can get the authenticated user ID in your Blade template by using the `auth()` helper function provided by Laravel. Here's how you can do it in your `navigation.blade.php` file:

php
@if(auth()->check())
Profile
@endif


This code snippet checks if the user is authenticated using the `auth()->check()` function and then generates a link to the profile route using the `auth()->id()` function to get the authenticated user's ID. Make sure to replace `'profile'` with the actual name of your profile route.

Rate this post

5 of 5 based on 9394 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us