我对Laravel和JSON都是新手。我必须给用户一个删除帐户的选项,但是我不知道怎么做。这是我尝试使用类似更新表单的方法的代码,但是我失败了。
编辑:我找到了一些解决方案,但它没有删除用户,表名是"users"。
第二次编辑:我尝试了更多的东西,不知何故,它确实删除了用户几次,但后来我想,由于laravel cookie文件,我仍然能够登录,但没有注册说,我的帐户存在,然后我再次检查数据库,我的电子邮件在那里与一个随机用户名,没有图像,和不同的id。我尝试删除cookie,但没有一个工作。我甚至不知道什么是文件的名称或做什么来删除它。更新也使用firebase-auth。
public\assets\js\common.js
$(".delteuserbtn").on('click', function (e1){
e1.preventDefault();
$('.deltetemodal').modal('show');
$(".delteuserbtnyes").on('click', function (e2){
e2.preventDefault();
var theuser = localStorage.getItem('userObject');
if(theuser != null){
theuser = JSON.parse(theuser);
userid=theuser.id;
}
console.log(userid);
$.ajaxSetup({
headers: {
'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
}
});
$.ajax({
type: "DELETE",
url: "deleteAccount/"+userid,
dataType: "JSON",
success: function (response){
console.log('response it worked');
$(".deltetemodal").modal('hide');
$(".updateProfileModal").modal('hide');
localStorage.removeItem("userObject");
$('.userName').text("")
$('.useremail').text("")
$(".usersideImage").attr('src',"")
$('.signInRemove').removeClass('d-none');
$(".userLoginDiv").css("display", "none");
iziToast.success({timeout: 2000, title: localAccDeletedSuccess,position: "topRight"})
window.location.href = `${appUrl}`;
},
error: function(xhr) {
console.log(xhr.responseText); // this line will save you tons of hours while debugging
// do something here because of error
}
});
});
$(".delteuserno").on('click', function (e){
$(".deltetemodal").modal('hide');
$(".updateProfileModal").modal('hide');
});
});
$("#updateform").on('submit',function(event) {
event.preventDefault();
$(".loader").show();
var updatedata = localStorage.getItem('userObject');
updatedata = JSON.parse(updatedata);
var formdata = new FormData($("#updateform")[0]);
formdata.append('id',updatedata.id);
console.log(formdata);
$.ajax({
url: `${baseUrl}updateProfile`,
type: 'POST',
beforeSend: function(xhr) {
xhr.setRequestHeader('apikey',apikey);
xhr.setRequestHeader('userId',updatedata.id);
},
data: formdata,
dataType: "json",
contentType: false,
cache: false,
processData: false,
success: function(response) {
console.log(response)
$(".loader").hide();
iziToast.success({timeout: 2000, title: `${localUpdateSuccessfull}`,position: "topRight"})
localStorage.setItem('userObject',JSON.stringify(response.data));
$image = `${fimageUrl}${response.data.image}` ;
$(".usersideImage").attr('src',$image)
$('.userName').text(response.data.firstname)
$('.useremail').text(response.data.email)
$('.updateProfileModal').modal('hide');
},
error: function(err) { console.log(JSON.stringify(err)); }
});
});
这是我的blade.php资源\视图\包含\应用程序. blade.php
<div class="modal fade updateProfileModal" tabindex="-1" role="dialog" aria-labelledby="exampleModalCenterTitle"
aria-hidden="true" dir="{{__('app.ltr')}}">
<div class="modal-dialog modal-dialog-centered modalSm" role="document">
<div class="modal-content darkthemejs {{ $theme . '-theme' }}">
<h1 class="upTitle darkthemejs {{ $theme . '-theme' }}">{{__('app.EditProfile')}}</h1>
<form action=" " class="updateForm" id="updateform">
<div class="d-flex justify-content-center align-items-center flex-column">
<div class="updateProdiv">
<img src="https://images.unsplash.com/photo-1531427186611-ecfd6d936c79?ixid=MnwxMjA3fDB8MHxzZWFyY2h8MTF8fHByb2ZpbGV8ZW58MHx8MHx8&ixlib=rb-1.2.1&auto=format&fit=crop&w=500&q=60"
class=" updateProfileImage" alt="">
</div>
<label for="userPicfile" class="upUpdateIconBtn"><i class="fas fa-pencil-alt"></i></label>
<input type="file" class="d-none" name="image" id="userPicfile" accept="image/x-png,image/gif,image/jpeg">
</div>
<div class="d-flex justify-content-center align-items-center flex-column mt-3">
<div class="form-group upInputMain ">
<label class="UPlabel darkthemejs {{ $theme . '-theme' }}">{{__('FullName')}}</label>
<input type="text" name="firstname" class="form-control upinput upfullname darkthemejs {{ $theme . '-theme' }}" required>
</div>
<div class="form-group upInputMain">
<label class="UPlabel darkthemejs {{ $theme . '-theme' }}">{{__('E-mail')}}</label>
<input type="email" name="email" class="form-control upinput upemail darkthemejs {{ $theme . '-theme' }}" required>
</div>
<div>
<button type="submit" value="Update" class="updateBtn">{{__('app.UPDATE')}}</button>
</div>
</div>
</form>
<button type="submit" value="Delete" id="delteuser" class="delteuserbtn">{{__('app.Delete')}}</button>
</div>
</div>
</div>
<div class="modal fade deltetemodal" tabindex="-1" role="dialog" aria-labelledby="exampleModalCenterTitle"
aria-hidden="true" dir="{{__('app.ltr')}}">
<div class="modal-dialog modal-dialog-centered modalSm" role="document">
<div class="modal-content darkthemejs {{ $theme . '-theme' }}">
<h1 class="upTitle darkthemejs {{ $theme . '-theme' }}">{{__('app.areyousure')}}</h1>
<p>all of your data will be delted. are you sure?</p>
<div class="deleteuserbtns">
<button id="delteuseryes" class="delteuserbtnyes deleteRecord">{{__('app.yes')}}</button>
<button id="delteuserno" class="delteuserbtnno">{{__('app.no')}}</button>
</div>
</div>
</div>
</div>
routes/web.php
<?php
use App\Http\Controllers\ViewController;
use Illuminate\Support\Facades\Artisan;
use Illuminate\Support\Facades\Route;
Route::delete('deleteAccount/{id}' , [App\Http\Controllers\UserController::class, 'destroy']) ->name('destroy');
http/Controller/userController.php
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\User;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\DB;
class UserController extends Controller {
public function destroy($id){
/* $user = users::find($id);
$user ->delete(); */
DB::table('users')->where('id', $id)->delete();
return response()->json([
'status' =>200,
'message'=>'Account Deleted Successfully',
]);
/* $directory=storage_path('framework/views');
$files=\File::allFiles($directory);
\File::delete($files); */
}
}
1条答案
按热度按时间llycmphe1#
这是我在控制台中得到的,现在它实际上正在更新电子邮件名称,这很奇怪,我使用相同的代码,因为我liturly复制它。
我还注意到我的控制器里没有这一行。
所以我加了
这是我在控制台中得到的“错误”。
我还注意到更新用户名和电子邮件在 AJAX url中使用firebase-auth。
我尝试的事情,从互联网上我尝试了不同的方法或路线,并改变了删除用户的方式。没有工作不幸的是,