简体   繁体   English

如何使用用户帐户删除与用户相关的所有内容

[英]How do I delete everything related to user with the user account

I want to delete the user account as well as I want check if the user contains any 'posts', 'messages', 'friends' and if it does I want it to delete them as well.我想删除用户帐户以及检查用户是否包含任何'posts', 'messages', 'friends' ,如果有,我也希望它删除它们。
I haven't made any relations in their models.我没有在他们的模型中建立任何关系。
I am having hard time develop this logic can anyone help me with it.我很难开发这种逻辑,任何人都可以帮助我。

Here's what I have done so far which dosen't works:-这是我到目前为止所做的但不起作用: -

public function delete()
{
    $delete = User::find(Auth::user()->id);

    $post = DB::table('posts')
        ->where('created_by', '=', Auth::user()->uuid)
        ->get();

    $messages = DB::table('messages')
        ->where('from', '=', Auth::user()->id)
        ->where('to', '=', Auth::user()->id)
        ->get();

    $friend = DB::table('friends')
        ->where('my_id', '=', Auth::user()->id)
        ->where('friends_id', '=', Auth::user()->id)
        ->get();
  
     $delete->delete();
     $post->delete();
     $message->delete();
     $friend->delete();
}

get() will give you an array, so delete() may not work after you use ->get . get()会给你一个数组,所以delete()在你使用后可能不起作用->get

Try this:尝试这个:

$delete = User::find(Auth::user()->id);
$delete->delete(); // Example of Model

$post = DB::table('posts')
    ->where('created_by', '=', Auth::user()->uuid)
    ->delete();

$messages = DB::table('messages')
    ->where('from', '=', Auth::user()->id)
     ->where('to', '=', Auth::user()->id)
     ->delete();

$friend = DB::table('friends')
    ->where('my_id', '=', Auth::user()->id)
    ->where('friends_id', '=', Auth::user()->id)
    ->delete();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 当用户删除与通知相关的评论时,如何删除 laravel 中的通知 - how do i delete a notification in laravel when a user deletes the comment related to the notification 如何在删除帐户中删除用户的帖子? - How to delete posts of user at delete account? 用于删除用户帐户的按钮 - Button to delete a user account 如何轻松存储用户帐户详细信息? - How do I easily store user account details? 如何保存每个用户帐户的复选框? - How do i save checkboxes ticked for each user account? 如何在删除用户之前为用户添加确认消息? - How do I add a confirmation message for the user before they delete? 如何获取包括memberID的登录用户帐户数据,以便用户可以编辑或删除其帐户? - How to get a logged in user account data including memberID so user can edit or delete their account? 如何删除本地主机上的上传文件以及PHP中的用户帐户 - How To Delete Uploaded File On Local Host Along With User Account In Php PHP和MySQL-当用户删除其帐户时,以及如果用户未登录两周后,如何删除我的数据库呢? - PHP & MySQL - How can I flag my database when a user deletes their account and after two weeks if the user has not logged in really delete the account 如何回显变量然后删除页面上的所有内容并重复 - How do I echo a variable then delete everything on the page and repeat
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM