[英]Updating database records from a form using Laravel
I am new to Laravel and I am trying to update a grantor's details via a form, however the record in the database table is not registering any changes.我是 Laravel 的新手,我正在尝试通过表格更新设保人的详细信息,但是数据库表中的记录没有记录任何更改。 I have tried a number of different solutions but unfortunately I've not been successful yet - I have a feeling the issue may involve the update method in the controller not being reached, as when I place a return/redirect statement in the update method this seems to be ignored.我尝试了许多不同的解决方案,但不幸的是我还没有成功 - 我感觉问题可能涉及未达到 controller 中的更新方法,就像我在更新方法中放置返回/重定向语句时一样似乎被忽略了。 The edit method appears to work correctly, however the update method does not alter the database record. edit 方法似乎可以正常工作,但是 update 方法不会改变数据库记录。 I will include snippets of my existing code for clarity.为了清楚起见,我将包含我现有代码的片段。 Any help with this problem would be greatly appreciated!任何有关此问题的帮助将不胜感激!
Grantor Controller File授予者 Controller 文件
public function update(GrantorRequest $request, $id)
{
$grantor = Grantor::findOrFail($id);
$request->validate([
'grantor_name' => 'required'
]);
$updateDetails = [
'grantor_name' => $request->get('grantor_name')
];
$grantor->update($updateDetails);
return redirect()->route('grantors.index');
}
Note 'grantor_name' is an example of one field in the database row, there are multiple others which would also require updating.注意“grantor_name”是数据库行中一个字段的示例,还有多个其他字段也需要更新。
Grantors.edit.blade File Grantors.edit.blade 文件
<div class="flex-1 px-10">
<form method="PUT" action="{{ route('grantors.update', $grantor->id) }}" class="is-readonly">
@method('PATCH')
@csrf
<div class="row">
@if ($errors->any()) <span>{{ $errors }}</span> @endif
</div>
<div class="h-auto md:h-96">
<!-- item -->
<div x-data=" {isOpen : false} " class="pt-2 pb-4 border-b border-divider">
<div @click="isOpen = !isOpen"
class="flex items-center cursor-pointer text-neutral-darker hover:text-primary-hover"
:class="{'font-bold' : isOpen}">
<span class="text-xl md:xl">Grantor Details</span>
<img class="mb-1 duration-300 ml-4 inset-0 h-6 w-6" :class="{'transform rotate-180' : isOpen}"
src="../../img/icon-arrow-down.png" alt="missing">
</div>
<div x-show.transition.duration.300ms.origin.bottom="isOpen" x-cloak @click.away="isOpen = false"
class="pt-3 text-sm text-neutral">
@if($is_editable)
<div class="flex justify-end">
<button type="button" class="btn btn-default btn-edit js-edit"><img class="mb-1 duration-300 ml-4 inset-0 h-6 w-6" src="../../img/edit-icon.svg" alt="edit"></button>
<button type="button" class="btn btn-default btn-save js-save"><img class="mb-1 duration-300 ml-4 inset-0 h-6 w-6" src="../../img/save-icon.svg" alt="save"></button>
</div>
@endif
<form class="w-full max-w-sm">
<div class="md:flex md:items-center mb-6 ml-6">
<div class="md:w-1/7">
<label for="grantor_name" class="block text-neutral-darker font-bold md:text-right mb-1 md:mb-0 pr-4 " for="inline-full-name" disabled>Grantor/ Company/ Organisation Name *
</label>
</div>
<div class="md:w-1/6">
<input class="bg-gray-200 appearance-none border-2 border-gray-200 rounded w-full py-2 px-4 text-gray-700 leading-tight focus:outline-none focus:bg-white focus:border-green-900 is-disabled @error('grantor_name') is-invalid @enderror" name="grantor_name" value="{{ old('grantor_name') ?? $grantor->grantor_name }}" id="grantor_name" type="text" disabled >
</div>
</div>
Tenant Routes File租户路由文件
Route::get('/grantors/{id}/edit', [App\Http\Controllers\GrantorController::class, 'edit'])->name('grantors.edit');
Route::put('/grantors', [App\Http\Controllers\GrantorController::class, 'update'])->name('grantors.update');
Grantor Model授予人 Model
protected $fillable= [
'grantor_name'
]);
Note that there are many other fields included in the $fillable which I have not included for purposes of length.请注意,$fillable 中包含许多其他字段,出于长度的目的,我没有包含这些字段。
Because you're not actually updating the model.因为您实际上并没有更新 model。 You need to call update
on your model:您需要在 model 上调用update
:
public function update(GrantorRequest $request, $id)
{
// Dont do `toArray` . You'll lose the Eloquent model.
$grantor = Grantor::findOrFail($id);
$request->validate([
'grantor_name' => 'required'
]);
$updateDetails = [
'grantor_name' => $request->get('grantor_name')
];
// Notice the call below
$grantor->update($updateDetails);
return redirect()->route('grantors.index');
}
The route definition should be changed as well:路由定义也应该改变:
// `id` param was missing
Route::patch('/grantors/{id}', [App\Http\Controllers\GrantorController::class, 'update'])->name('grantors.update');
And html forms do not support patch
or put
method.并且 html forms 不支持patch
或put
方法。 You should do it like below:你应该这样做:
<form method="POST" action="{{ route('grantors.update', $grantor->id) }}" class="is-readonly">
@method('PATCH')
...
Grantors.edit.blade.php
File:更改您的Grantors.edit.blade.php
文件:<div class="flex-1 px-10">
<form method="POST" action="{{ route('grantors.update', ['id' => $grantor->id]) }}" class="is-readonly">
@method('PUT')
@csrf
// ...
Notice the changes:注意变化:
1a) method="PUT"
to method="POST"
1a) method="PUT"
到method="POST"
1b) @method('PATCH')
to @method('PUT')
1b) @method('PATCH')
到@method('PUT')
1c) {{ route('grantors.update', $grantor->id) }}
to {{ route('grantors.update', ['id' => $grantor->id]) }}
1c) {{ route('grantors.update', $grantor->id) }}
到{{ route('grantors.update', ['id' => $grantor->id]) }}
`` ``
// ...
Route::put('/grantors/{id}', [App\Http\Controllers\GrantorController::class, 'update'])->name('grantors.update');
// ...
2a) Included the /{id}
segment in the route. 2a) 在路由中包含/{id}
段。
<form>
tag in your Grantors.edit.blade.php
file.确保您的Grantors.edit.blade.php
文件中有一个<form>
标签。 From the looks of it, you have 2 <form>
tags.从外观上看,您有 2 个<form>
标签。 One nested inside another.一个嵌套在另一个内部。
<form method="PUT" action="{{ route('grantors.update', $grantor->id) }}" class="is-readonly">
and和
<form class="w-full max-w-sm">
In your terminal, run the command below:在您的终端中,运行以下命令:
php artisan cache:clear
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.