Skip to content

Commit c6c7298

Browse files
committed
Add delete photo button
1 parent 65ce882 commit c6c7298

File tree

6 files changed

+62
-0
lines changed

6 files changed

+62
-0
lines changed

Diff for: routes/inertia.php

+3
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@
2222
Route::delete('/user', [CurrentUserController::class, 'destroy'])
2323
->name('current-user.destroy');
2424

25+
Route::delete('/user/profile-photo', [CurrentUserController::class, 'deleteProfilePhoto'])
26+
->name('current-user-photo.destroy');
27+
2528
// API...
2629
if (Jetstream::hasApiFeatures()) {
2730
Route::get('/user/api-tokens', [ApiTokenController::class, 'index'])->name('api-tokens.index');

Diff for: src/HasProfilePhoto.php

+14
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,20 @@ public function updateProfilePhoto(UploadedFile $photo)
2828
});
2929
}
3030

31+
/**
32+
* Delete the user's profile photo.
33+
*
34+
* @return void
35+
*/
36+
public function deleteProfilePhoto()
37+
{
38+
Storage::disk($this->profilePhotoDisk())->delete($this->profile_photo_path);
39+
40+
$this->forceFill([
41+
'profile_photo_path' => null,
42+
])->save();
43+
}
44+
3145
/**
3246
* Get the URL to the user's profile photo.
3347
*

Diff for: src/Http/Controllers/Inertia/CurrentUserController.php

+13
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,19 @@
1111

1212
class CurrentUserController extends Controller
1313
{
14+
/**
15+
* Delete the current user profile photo.
16+
*
17+
* @param Request $request
18+
* @return \Illuminate\Http\RedirectResponse
19+
*/
20+
public function deleteProfilePhoto(Request $request)
21+
{
22+
$request->user()->deleteProfilePhoto();
23+
24+
return back()->with('status', 'profile-photo-deleted');
25+
}
26+
1427
/**
1528
* Delete the current user.
1629
*

Diff for: src/Http/Livewire/UpdateProfileInformationForm.php

+14
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,20 @@ public function updateProfileInformation(UpdatesUserProfileInformation $updater)
5959
$this->emit('saved');
6060
}
6161

62+
/**
63+
* Delete user's profile photo.
64+
*
65+
* @param \Laravel\Fortify\Contracts\UpdatesUserProfileInformation $updater
66+
*
67+
* @return void
68+
*/
69+
public function deleteProfilePhoto(UpdatesUserProfileInformation $updater)
70+
{
71+
Auth::user()->deleteProfilePhoto();
72+
73+
return redirect()->route('profile.show');
74+
}
75+
6276
/**
6377
* Get the current user of the application.
6478
*

Diff for: stubs/inertia/resources/js/Pages/Profile/UpdateProfileInformationForm.vue

+12
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,10 @@
3434
Select A New Photo
3535
</jet-secondary-button>
3636

37+
<jet-button type="button" @click.native.prevent="deleteProfilePhoto" v-if="$page.user.profile_photo_path">
38+
Delete Photo
39+
</jet-button>
40+
3741
<jet-input-error :message="form.error('photo')" class="mt-2" />
3842
</div>
3943

@@ -103,6 +107,14 @@
103107
},
104108
105109
methods: {
110+
deleteProfilePhoto() {
111+
this.$inertia.delete('/user/profile-photo', {
112+
preserveScroll: true,
113+
}).then(() => {
114+
this.photoPreview = null
115+
});
116+
},
117+
106118
updateProfileInformation() {
107119
if (this.$refs.photo) {
108120
this.form.photo = this.$refs.photo.files[0]

Diff for: stubs/livewire/resources/views/profile/update-profile-information-form.blade.php

+6
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,12 @@
4242
Select A New Photo
4343
</x-jet-secondary-button>
4444

45+
@if($this->user->profile_photo_path)
46+
<x-jet-button type="button" wire:click="deleteProfilePhoto">
47+
Delete Photo
48+
</x-jet-button>
49+
@endif
50+
4551
<x-jet-input-error for="photo" class="mt-2" />
4652
</div>
4753
@endif

0 commit comments

Comments
 (0)