Friday, March 31, 2017

Laravel string is not entering/updating in the database

First is it checks if request form has the same username in the database and then the first name that is saved in the database to be compared with the request form. But it is not entering in the database I may have done something wrong when turning the strings into arrays or using the for loop to compare both strings but I can't spot the error can you help me?

if(User::where('username', $request->username)->exists()) {//check if request form has the same username in the database
              //first name that is saved in the database to be compared with the request form
                $fnameSavedInDatabase = DB::select('select first_name from users where username = ? and first_name != ?', [$request->username, $request->first_name]);
              //turn into array
                $fnameSavedInDatabaseArray = str_split($fnameSavedInDatabase);
               print_r($fnameSavedInDatabaseArray);
              //request form value turn in to an array
                $fnameIncomingArray = str_split($request->first_name);
              //value that will be saved/updated in the database
                $fnameFinalIncomingString;

                for($index = 0; $index < strlen($request->first_name); $index++) {
                  if($fnameIncomingArray[$index] == $fnameSavedInDatabaseArray[$index]){
                    $fnameFinalIncomingString = $fnameFinalIncomingString . $fnameIncomingArray[$index];
                  }
                }

                $user = User::create($request->all());

                $user->update(['role_id' => $request->role]);
                $user->save();
                $user->update(['username' => $fnameFinalIncomingString]);
                $user->save();

                $roleName = $user->role()->name;

                return response()->json(['user' => $user, 'roleName' => $roleName,'manager_exists'=>'0']);



via user827391012

Advertisement