php_dev
php_dev

Reputation: 53

How to make when user is deleted that all of his posts reference to Admin with user_id Laravel

Hello there Laravel Developers, I need help..

I have two tables:

users with columns:

id

name

email

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

Answers (1)

Dibyayan Chatterjee
Dibyayan Chatterjee

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

Related Questions