Reputation: 53
Hello there Laravel Developers, I need help..
I have two tables:
users
with columns:
id
name
password
and I have table posts
with columns:
title
body
user_id
How to do that when a certain user is deleted, all his posts (which are related to him by user_id) point to the Administrator who deleted that user? I assume I'm using a foreign key or something ...?
Please help, I'm a beginner at Laravel .. Thanks.
Upvotes: 0
Views: 60
Reputation: 36
Try This hopefully its works for you
public function removeUser($id){
$res=User::find($id)->delete();
if ($res){
$values = Post::where('user_id ', $id)->update(['user_id
'=>'your_admin_id']);
return redirect()->back()->with('status','User Id Deleted
Successfully');
}else{
// Do Something
}
}
You can find your admin id from session
Upvotes: 0