0

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!

eitibiti
  • 45
  • 6
  • 1
    A `/profile` route should not have any URL parameters, otherwise, you could do `/profile/1`, and modify User 1's details, even if that isn't you. Separate your routes into a `/user/{id}` and a `/profile`, and in the Controller method for that Profile route, use `auth()->user()`. Also, if you're not logged in, make the `/profile` route unavailable. – Tim Lewis Feb 25 '22 at 17:57
  • All the profiles should be accessible by all users, I already have it set so only the profile owner could edit its info, otherwise you'll get a 403. Just wanna insert it's link on navigation. – eitibiti Feb 25 '22 at 18:00
  • 2
    Can you show the actual route? – aynber Feb 25 '22 at 18:00
  • Gotcha. In that case, `{{ route('profile', auth()->user()->id) }}` _should_ work. You say it doesn't, but what does that mean, doesn't work? Alternatively, if you made that param Optional, like `/profile/{id?}`, and it was omitted, then you would know you're viewing your own profile. Also, it might be `{{ route('profile.profile', ...) }}`, since you're defining a `prefix` and `as` on your `Route::group()` – Tim Lewis Feb 25 '22 at 18:03
  • It just throws a sintax error on the line, already tried the profile.profile approach. Seems that on my layouts I can't use the auth directive and can't get the current user id. – eitibiti Feb 25 '22 at 18:09
  • As long as they are `.blade.php` files, you _should_ be able to use `{{ route() }}`, and the `auth()->user()` function. Can you please provide the specific error message you're receiving? And if you want to confirm the Route, run `php artisan route:list`, and look at the `Name` column. Anything there can be used in the `route()` method. – Tim Lewis Feb 25 '22 at 18:11
  • 1
    Try `"{{ route('profile', ['id' => Auth::user()->id]) }}"` But what syntax error are you getting? – aynber Feb 25 '22 at 18:11
  • manage to get it working guys, happens that i didn't needed the ':' on the href, that was causing the error – eitibiti Feb 25 '22 at 18:18

1 Answers1

0

Credits to @aynber to answering, what i needed to do was:

{{ route('profile', ['id' => Auth::user()->id]) }}
eitibiti
  • 45
  • 6