[英]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.