更新功能的按钮不工作laravel 8(CRUD)



我是laravel 8的新手。当用户已经编辑了他们的数据并将其存储在数据库中时,我尝试制作一个更新功能控制器。我试着点击提交按钮,当我尝试在chrome上检查时,它没有任何原因地工作。所以我尝试了另一种方式,即评论$request->validate,这只是名称数据的更改,而organization_id为空并返回NULL。我想知道我们是否需要始终验证更新功能中的数据?

这是我的控制器

public function edit($id)
{
$record = User::find($id);
$org = Organisation::all();
$t = Organisation::pluck("name");
return view ('user.edit',compact('record','org','t'));
}
public function edit_store(Request $request, $id)
{
$request->validate([
'name' => 'required',
'organisation_id' => 'required',
]
);
$record= User::find($id);
$record->name = $request->name;
$record->organisation_id = $request->organisation_id;
$record->save();
return redirect ('user')->with('success', 'Thank you');
}

这是我的编辑刀片

<div class="card">
<div class="card-body">
<form action="{{route('user_edit_store1', $record->id)}}" method="POST">
@csrf
<input type="hidden" name="id" value="{{ $record->id }}">
<div class="form-group row">
<label for="name"
class="col-md-4 col-form-label text-md-right">{{ __('Name') }}</label>
<div class="col-md-6">
<input id="name" type="text"
class="form-control @error('name') is-invalid @enderror" name="name"
value="{{ old('name') ? old('name') : $record->name }}" 
</div>
</div>
<div class="form-group row">
<label for="organisation_id"
class="col-md-4 col-form-label text-md-right">{{ __('Organisation') }}</label>
<div class="col-md-6">
{!! Form::open(['route' => 'user_store']) !!}
{!! Form::select('id', $t, 'id',  ['class' => 'form-control']) !!}
</div>
</div>
<div class="form-group row mb-0">
<div class="col-md-6 offset-md-4"> 
<button type="submit" class="btn btn-primary" >
{{ __('Save') }} 
</button>
</div>
</div>
</form>
</div>
</div>

路由

Route::get('user_edit/{id}',[UserController::class,'edit'])->name('user_edit');
Route::post('user_edit_store/{user}',[UserController::class,'edit_store'])->name('user_edit_store1');

模型用户.php

protected $fillable = [
'name',
'organisation_id',
];

您使用Form::select表单助手创建的<select>的名称将为"id",而不是"organization_id",这将导致验证失败。

最新更新