首先,它检查请求表单是否在数据库中具有相同的用户名,然后检查数据库中保存的第一个名称是否与请求表单进行比较。但它没有进入数据库我可能在将字符串转换为数组或使用for循环来比较两个字符串时做错了但我无法发现错误你能帮助我吗?
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);
//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']);
}
答案 0 :(得分:0)
检查您的模型用户。 它必须在
protected $fillable = ['username',....]
然后在你的dev envirenoment中使用修补程序调试更新用户名而没有乱七八糟。
in cmd
php artisan tinker
然后
$user = \App\User::find(1);
$user->update(['username' => 'mytestname']);