I have an active database with username columns but I later discovered it has a lot of whitespace which causes a not found error when I try to compare a record against itself. So I am trying to remove all white space from all user names, here is the challenge:
$user = (User::where("unique_id", 1)->first())->username;
$string = preg_replace('/\s+/', '', $user);
$dd = DB::table("users")->where("unique_id", 1)
->update(["username"=>$string]);
Where I select a single query like the above and change the the username it works,
but when I try to change the whole usernames in the database at once it does not work.
$user = User::all();
foreach($user as $user){
$username = preg_replace('/\s+/', '', $user->username);
// $sponsor = preg_replace('/\s+/', '', $user->sponsor_username);
DB::table("users")->where("unique_id", $user->id)->update(["username"=>$username]);
}