Doubts about method EDIT, UPDATE and routes

0

I'm working with the default auth of laravel, the question I have is that to edit the profile, is it necessary to pass the user's $ id in the edit form? this $ id is captured by the EDIT method of the UserController and then by the UPDATE method, can only auth () be taken from the form to the controller? Is there any way to make the urls cleaner?

I am new to laravel and I have to make a small application that will later have an api of

I enclose screenshot of the user profile, form and controller routes

Routes

Controller method Edit

Driver method Update

Form

Thanks for the help!

    
asked by Felipe 19.03.2018 в 20:31
source

3 answers

0

Urls are clean, what you are wanting to do is not to pass id to edit or update and that is logical since that could only be done by the authenticated user.

What you have to do is:

// Route

Route::get('panel/profile', 'UserController@edit');
Route::put('panel/profile', 'UserController@update');

// Controller

public function edit() {
  $user = auth()->user(); // si el usuario esta autenticado te devuelve el User
  ...
}

public function update(Request $request)
  $user = auth()->user();
  ...
}

I recommend you add the auth middleware to your controller's constructor

public function __construct()
{
    $this->middleware('auth');
}
    
answered by 19.03.2018 / 20:55
source
1

Is it necessary to pass the user's $ id in the edit form?

I suppose you refer to the update form, in that case, yes, it is, otherwise you would not know which user you are going to update, unless you always call the user logged in by Auth .

Can only use auth () from the form to the controller?

Yes, you can call the logged-in user using Auth::user()->id .

Is there a way to make the urls cleaner?

Laravel uses friendly routes, they are as clean as possible in my opinion, unless your conception of 'clean' is another.

    
answered by 19.03.2018 в 20:46
1

Yes there is a way to make your url cleaner.

For example:

Route::resource('nombredelaruta','NombreControlador')

With this single line if you go to the console and execute php artisan route:list , You will see the 4 basic URLs such as Get, Post, Put and Delete.

    
answered by 19.03.2018 в 21:57