I am giving the option to the user to decide if he wants to delete his account or not, and I can't seem to find a way to run (or not) the function to delete the account according to his choice... here is the code:
firstly I thought of adding to ProfileController something like if $form['confirm'] = 'yes', run function
but it didn't work...
web.php
Route::get('delete_account/{id}', 'ProfileController@deleteProfile');
Route::post('deleteAccount', 'ProfileController@delete')->name('deleteAccount');
HTML (pages.delete_account)
<div class="big">
<form method="POST" action="{{ route('deleteAccount') }}">
<label for="yes">Yes</label>
<input id="yes" type="radio" name="confirm" value="{{ old('yes') }}" required autofocus>
<label for="no">No</label>
<input id="no" type="radio" name="confirm" value="{{ old('no') }}" required autofocus>
<button type="deleteAccount">
Confirm
</button>
</div>
ProfileController.php
public function deleteProfile($id)
{
if (!Auth::check()) return redirect('/login');
return view('pages.delete_account');
}
public function delete(Request $request)
{
$user = Events::find(Auth::user()->id);
$this->authorize('delete', $user);
$user->delete();
return redirect('/login');
}
Are there any other parts of the code you need to fully understand the question? Please let me know :)
My Answer:
Based on your code, it looks like you are trying to delete a user account based on the user's confirmation in the form. Here are a few suggestions to make it work:
1. In your HTML form, you need to make sure that the value of the radio buttons corresponds to the user's choice. For example, for the "Yes" option, the value should be "yes" and for the "No" option, the value should be "no".
2. In your ProfileController, you can check the value of the "confirm" parameter from the form submission and then decide whether to delete the user account or not. Here is an updated version of your delete function:
php
public function delete(Request $request)
{
if ($request->confirm == 'yes') {
$user = Events::find(Auth::user()->id);
$this->authorize('delete', $user);
$user->delete();
return redirect('/login');
} else {
return redirect()->back()->with('message', 'Account deletion cancelled.');
}
}
With this updated function, if the user selects "Yes" in the form, the account will be deleted. If the user selects "No" or does not select any option, the account deletion will be cancelled.
Make sure to also handle the case where the user does not select any option in the form submission.
I hope this helps! Let me know if you have any further questions.
Rate this post
4 of 5 based on 1715 votesComments