Daniel-Pablo's avatar

advise creating a profile user route

Hi community, I am new on laravel, I am creating an user with a profile made this route

Route::resource('/profile', 'ProfileController');

For some reazon I thing this is not the correct way to do this stuff, because I will need to call every single time the user ID to show the profile of the user on a PROFILE PAGE LIKE THIS WAY

/profile/5/show // this way I will see the profile number 5 BUT

the thing is I AM USER 5 , so I thing I need to enter onto

/user/5/profile // this should make the work...

or in the ProfileController I should USE USER then call

user->profile...

Please get me into the right spot, please, how is the right way to manage this because I need to create PRODUCTS CONTACT AND MONEY of the user Controllers thanks

0 likes
4 replies
Sinnbeck's avatar

The route you are showing there would be to edit an arbitrary user. If you plan to edit the signed in user, you can instead make your own routes that does not require an ID in the url

Route::get('profile', 'ProfileController@show');
Route::get('profile/edit', 'edit');
Route::put('profile', 'ProfileController@update');
Route::delete('profile', 'ProfileController@destroy');

Each of these would then just get the correct user using Auth::getUser()

Daniel-Pablo's avatar

@sinnbeck Thanks Sr.I really use your way, am it helps me a lot but I end up with this

public function index(Request $request)
{
   $profile= $request->user()->profile()->get()->first();
    return view('profile.index' , compact('profile'));
}

This way the controller calls the profile of the authenticated user and shows its profile attached as a relation ship

Do you think is the right way... let me know your thoughts about it, and thanks again for your help

Sinnbeck's avatar
Sinnbeck
Best Answer
Level 102

Yes that is fine, but can be simplified a bit as I assume that a user only have one profile

$profile = $request->user()->profile;
Daniel-Pablo's avatar

Thanks a lot!! wowww that is so shorter and make it done!! Thanks!

Please or to participate in this conversation.