Unset all fields except a known field(s) in Laravel?

Solution:

Unset all of them except the one you require,

public static function DeleteAccount($id){
    $user = User::find($id);
    $req = ['_id','created_at','updated_at'];
    foreach($user->toArray() as $key => $value){
        // not removing _id,created_at and updated_at
        if(!in_array($key,$req)){
            $user->unset([$key]);
        } 
    }
    $user->save();
    dd($user); //will now have only _id,created_at and updated_at
}

Note : we cannot use unset($user[$key]) here as it will unset in the object but as you are using mongodb, it will not remove in the collection.