[英]Problem with updating data in database in Laravel
I have a form that has username, job, date of birth and city inputs.我有一个包含用户名、工作、出生日期和城市输入的表格。 I am trying to update them through my form.
我正在尝试通过我的表格更新它们。 When I click submit button my form submits but data remains unchanged.
当我单击提交按钮时,我的表单提交但数据保持不变。 I successfully read them from database but when I try and update nothing happens.
我成功地从数据库中读取了它们,但是当我尝试更新时没有任何反应。 Any help is appreciated.
任何帮助表示赞赏。 Here is my code.
这是我的代码。
UserController.php UserController.php
public function showProfile($username, Request $request)
{
$profileId = User::getIdFromUsername($username);
$userForShowProfile = User::with('userProfile')->where('id', $profileId)->first();
return view('profile.show', compact('userForShowProfile'));
}
public function updatePersonalData(UpdatePersonalDataRequest $request)
{
$user = Auth::user();
$request->validated();
$user->where('id', $user->id)->update(
[
'username' => $request->username,
'job' => $request->job,
'date_of_birth' => $request->date_of_birth,
'updated_at' => Carbon::now()
]
);
$city = City::where('name', $request['city'])->first();
if ($city != null && $city->count() > 0) {
$request->user()->city()->associate($city->id);
}
$request->user()->save();
return response()->json(null, 204);
}
web.php web.php
Route::get('profile/{profile}', 'UserController@showProfile')->name('profile.show');
Route::patch('profile/personal', 'UserController@updatePersonalData')->name('profile.update.personal.data');
show.blade.php show.blade.php
<section data-edit="generalInfo" class="editGeneralInfo">
<form action="{{ route('profile.update.personal.data') }}" method="POST" class="flex">
@method('PATCH')
@csrf
<div class="form-group">
<label for="" class="textBold">Name</label>
<input type="text" name="username" value="{{ $userForShowProfile->username }}">
</div>
<div class="form-group">
<label for="" class="textBold">Ort</label>
<input type="text" name="job" value="{{ $userForShowProfile->job }}">
</div>
<div class="form-group">
<label for="" class="textBold">Beruf</label>
<input type="text" name="city" value="{{ $userForShowProfile->city->name }}">
</div>
<div class="form-group mb-0">
<label for="" class="textBold">Geburtsdatum</label>
<input type="text" placeholder="dd/mm/yyyy" name="date_of_birth" value="{{ $userForShowProfile->date_of_birth }}">
<p class="infoText mt-2">Dein Geburtsdatum wird nicht öffentlich angezeigt.</p>
<p class="infoText">Wir ermitteln damit nur dein Alter</p>
</div>
<div class="form-group">
<label for="" class="textBold">Button</label>
<input type="submit" class="form-control" name="submit" value="BUTTON">
</div>
</form>
</section>
rules规则
public function rules()
{
return [
'username' => ['string', 'max:255', 'unique:users,username'],
'job' => ['string', 'max:255'],
'date_of_birth' => ['date', 'date_format:d.m.Y'],
'city' => ['string', 'max:255', 'exists:cities,name']
];
}
i have a same problem in my code the error is:我的代码中有同样的问题,错误是:
i save like this: $user->save();我这样保存:$user->save();
while i change the data using: $user and $user->profile当我使用以下方法更改数据时:$user 和 $user->profile
so i added in my code another line: $user->profile->save()所以我在我的代码中添加了另一行: $user->profile->save()
it work now现在可以了
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.