i can’t update table data in laravel 10 with controller resource
my code:
// controller
public function update(Request $request, string $id):RedirectResponse
{
//
$input = $request->validate([
'id'=>'required',
'name'=>'required',
'description'=>'required',
]);
$accounting_group = AccountingGroup::findOrFail($id);
$accounting_group->update($input);
return redirect()->route('accounting_group.index')->with(['success' => 'Data telah disimpan']);
}
<!-- route -->
Route::resource('accounting_group', AccountingGroupController::class)->except(['show']);
<!-- model -->
class AccountingGroup extends Model
{
use HasFactory;
protected $table = 'accounting_groups';
protected $fillable = [
'id',
'name',
'description',
];
protected $hidden = [];
public function transactionaccount(): HasMany
{
return $this->hasMany(TransactionAccount::class);
}
}
<form method="post" class="mx-2 p-4" action="{{ route('accounting_group.update', $accounting_group->id) }}">
@csrf
@method('put')
<div class="form-group">
<label for="id">ID Akun</label>
<input type="text" class="form-control" id="id" placeholder="ID accounting..." value="{{ old('id', $accounting_group->id) }}">
</div>
....
<div class="d-flex justify-content-end">
<button type="submit" class="btn btn-primary">Simpan</button>
</div>
</form>
i didn’t get eror display in laravel, i got refresh page where i clicked sumbit button and in mysql the data get no update
2
Answers
To allow browser send input, you need to add you
name
attribute in your input for.So you just need to add
name="id"
to your:so your code should be:
and don’t forgot to add
name
attribute to other fields likename="name"
andname="description"
When you are using Route::resource and call the show or update method then you will get the requested ID in the record in the controller function it is called property promotion
Here I have enhanced your code