Skip to content

Commit b7a507c

Browse files
committed
Add profile photo upload and deletion functionality
1 parent 6602f0e commit b7a507c

File tree

10 files changed

+8422
-9
lines changed

10 files changed

+8422
-9
lines changed

app/Http/Controllers/Settings/ProfileController.php

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
use Illuminate\Support\Facades\Auth;
1111
use Inertia\Inertia;
1212
use Inertia\Response;
13+
use Illuminate\Support\Facades\Storage;
1314

1415
class ProfileController extends Controller
1516
{
@@ -29,13 +30,25 @@ public function edit(Request $request): Response
2930
*/
3031
public function update(ProfileUpdateRequest $request): RedirectResponse
3132
{
32-
$request->user()->fill($request->validated());
33+
$user = $request->user();
34+
$user->fill($request->validated());
35+
36+
if ($user->isDirty('email')) {
37+
$user->email_verified_at = null;
38+
}
39+
40+
if ($request->hasFile('photo')) {
41+
// Delete old photo if exists
42+
if ($user->profile_photo_path) {
43+
Storage::disk('public')->delete($user->profile_photo_path);
44+
}
3345

34-
if ($request->user()->isDirty('email')) {
35-
$request->user()->email_verified_at = null;
46+
// Store new photo
47+
$photoPath = $request->file('photo')->store('avatars', 'public');
48+
$user->profile_photo_path = $photoPath;
3649
}
3750

38-
$request->user()->save();
51+
$user->save();
3952

4053
return to_route('profile.edit');
4154
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
3+
namespace App\Http\Controllers\Settings;
4+
5+
use App\Http\Controllers\Controller;
6+
use Illuminate\Http\RedirectResponse;
7+
use Illuminate\Http\Request;
8+
use Illuminate\Support\Facades\Storage;
9+
10+
class ProfilePhotoController extends Controller
11+
{
12+
/**
13+
* Delete the current user's profile photo.
14+
*/
15+
public function destroy(Request $request): RedirectResponse
16+
{
17+
$path = $request->user()->profile_photo_path;
18+
if ($path && Storage::disk('public')->exists($path)) {
19+
Storage::disk('public')->delete($path);
20+
}
21+
22+
$request->user()->profile_photo_path = null;
23+
$request->user()->save();
24+
25+
return to_route('profile.edit');
26+
}
27+
}

app/Http/Requests/Settings/ProfileUpdateRequest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ public function rules(): array
2525
'max:255',
2626
Rule::unique(User::class)->ignore($this->user()->id),
2727
],
28+
'photo' => ['nullable', 'image', 'mimes:jpg,jpeg,png', 'max:1024'],
2829
];
2930
}
3031
}

app/Models/User.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use Illuminate\Database\Eloquent\Factories\HasFactory;
77
use Illuminate\Foundation\Auth\User as Authenticatable;
88
use Illuminate\Notifications\Notifiable;
9+
use Illuminate\Support\Facades\Storage;
910

1011
class User extends Authenticatable
1112
{
@@ -33,6 +34,15 @@ class User extends Authenticatable
3334
'remember_token',
3435
];
3536

37+
/**
38+
* The attributes that should be appended to the model's array form.
39+
*
40+
* @var list<string>
41+
*/
42+
protected $appends = [
43+
'avatar',
44+
];
45+
3646
/**
3747
* Get the attributes that should be cast.
3848
*
@@ -45,4 +55,18 @@ protected function casts(): array
4555
'password' => 'hashed',
4656
];
4757
}
58+
59+
/**
60+
* Get the URL of the user's profile photo.
61+
*
62+
* @return string|null
63+
*/
64+
public function getAvatarAttribute(): ?string
65+
{
66+
if ($this->profile_photo_path) {
67+
return Storage::url($this->profile_photo_path);
68+
}
69+
70+
return null;
71+
}
4872
}

0 commit comments

Comments
 (0)