我想更新个人资料,当我更新记录时,我未更改的旧数据也会更新。
我想只更新那些已更改的值。不应更新未更改的值。
路线:
Route::post('/profile/settings/{user}', 'Admin\userController@UpdateProfileSettings')->name('profile.settings');
控制器方法:
public function UpdateProfileSettings(Request $request, User $user)
{
if (auth()->user()->id == $user->id) {
$user->update([
'name' => $request->name,
'qualification' => $request->qualification,
'institute' => $request->institute,
'gender' => $request->gender,
'speciality' => $request->speciality,
'address' => $request->address,
]);
return redirect()->back();
} else {
abort(403, "Unauthorized");
}
}
答案 0 :(得分:2)
您可以尝试以下
$name = $request->input('name', $user->name);
对于laravel 5.4:
还可以确保在TrimStrings
ConvertEmptyStringsToNull
和App\Http\Kernel
个中间件
对于laravel 5.3,您可以手动将这些中间件添加到项目中。
说明了添加TrimgStrings
here。
添加ConvertEmptyStringsToNull
将遵循相同的步骤..
创建新文件ConvertEmptyStringsToNull.php
<?php
namespace App\Http\Middleware;
class ConvertEmptyStringsToNull extends TransformsRequest
{
/**
* Transform the given value.
*
* @param string $key
* @param mixed $value
* @return mixed
*/
protected function transform($key, $value)
{
return is_string($value) && $value === '' ? null : $value;
}
}
通过添加到App\Http\Kernel.php
protected $middleware = [
\Illuminate\Foundation\Http\Middlewar\CheckForMaintenanceMode::class,
\App\Http\Middleware\TransformsRequest::class,
\App\Http\Middleware\TrimStrings::class,
\App\Http\Middleware\ConvertEmptyStringsToNull::class,
];