Skip to main content

User Area: Get/Update Profile

Premium
5 min read

As in every application, users should be able to update their profile. Personally, I like to separate two actions: change profile details and change password.

So, I vote for these API endpoints:

  • GET /profile - to view profile details
  • PUT /profile - to update name/email
  • PUT /password - to update the password

You could also make both PUT actions into one endpoint, with an if-else statement, but I personally like the separation of those concerns.


Get/Update Profile

Let's generate a Profile Controller - this time with two methods in it.

I will still keep the namespace of Auth because those features are related to the authenticated users.

php artisan make:controller Api/V1/Auth/ProfileController

This will be the code inside.

app/Http/Controllers/Api/V1/Auth/ProfileController.php:

namespace App\Http\Controllers\Api\V1\Auth;
 
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Http\Response;
use Illuminate\Validation\Rule;
 
class ProfileController extends Controller
{
public function show(Request $request)
{
return response()->json($request->user()->only('name', 'email'));
}
 
public function update(Request $request)
{
$validatedData = $request->validate([
'name' => ['required', 'string'],
'email' => ['required', 'email', Rule::unique('users')->ignore(auth()->user())],
]);
 
auth()->user()->update($validatedData);
 
return response()->json($validatedData, Response::HTTP_ACCEPTED);
}
}

Not sure I need to explain much here: in the show() method we just show a few fields of a logged-in user (we don't show any ID or password-sensitive fields), and in the update() method we validate the data, update the DB row and return the updated data as JSON.

Now, the most important part: how do we get that auth()->user() or $request->user() automatically?

The Full Lesson is Only for Premium Members

Want to access all of our courses? (30 h 09 min)

You also get:

55 courses
Premium tutorials
Access to repositories
Private Discord
Get Premium for $129/year or $29/month

Already a member? Login here

Comments & Discussion

GK
Gavin Kimpson ✓ Link copied!

the Postman request image should also say 'current_password' (the image doesn't show) with the string value of the current password e.g 'password' :)

PK
Povilas Korop ✓ Link copied!

Damn, I knew I missed something, this course was done in 3 refactorings of the code, I knew I would forget to reshoot some screenshot...

Thanks for the notice, well spotted! Maybe will find time to replicate the whole project and re-take the screenshot.

GK
Gavin Kimpson ✓ Link copied!

no worries we are all human :) it was actually good for me to debug it so I'm kinda glad but happy to help really enjoy your tutorials

R
rochmadnf ✓ Link copied!

when validating the "current_password" request. You add the current_password rule to the request. i looked in laravel documentation. the rule has a guard parameter but the case you are showing doesn't use that parameter. is there a reason behind this?

PK
Povilas Korop ✓ Link copied!

I think this guard parameter is optional and defaults to the "auth" I don't personally work with multi-guard systems so hard to comment more, sorry.

ZN
Zar Ni Phyoe ✓ Link copied!

In the Logout tutorial section there is spelling mistake "we know hot to make", just reminding :)

PK
Povilas Korop ✓ Link copied!

Thank you for spotting, fixed!

JN
Jemmeli Nejmeddine ✓ Link copied!

If i will do SPA with sanctum , i have to create all those endpoints (register, login, password ...) also ?

PK
Povilas Korop ✓ Link copied!

Yes probably, but you may choose to use Laravel Breeze Vue version that already has those endpoints, from what I remember.

JN
Jemmeli Nejmeddine ✓ Link copied!

Thank you , you are really my best example to follow Mr Povilas <3

We'd Love Your Feedback

Tell us what you like or what we can improve

Feel free to share anything you like or dislike about this page or the platform in general.