我可以请求选中的并查看它们,但我无法更新它。
在数据库中,我有表格类型,在类型 2 列中:rijbewijsnummer,rijbewijstype
如何更新数据库中新检查的?
我的控制器中的函数
public function Modify (Request $request) {
$user = Auth::user();
$type = Type::all();
$user->rijbewijsnummer = Request::input('rijbewijsnummer');
$type->rijbewijstype = Request::input('rijbewijstype');
$user->save();
return redirect()->route('home');
}
我认为我在这里做错了什么
public function details(Request $request)
{
$user = Auth::user();
$types = Type::where("rijbewijsnummer", "=", $user->rijbewijsnummer)->get();
foreach ($types as $type)
{
$rijbewijstype[]= $type->rijbewijstype;
}
$user->save();
return view('details', compact('user','rijbewijstype', 'types'));
}
视图:
<div class="col-md-6">
<div name="rijbewijstype">
<input type="checkbox" name="rijbewijstype[]" @foreach ($types as $type) @if ($type->rijbewijstype == "AM")) checked @endif @endforeach value="AM" >AM</input>
<input type="checkbox" name="rijbewijstype[]" @foreach ($types as $type) @if ($type->rijbewijstype == "A1") checked @endif @endforeach value="A1">A1</input>
<input type="checkbox" name="rijbewijstype[]" @foreach ($types as $type) @if ($type->rijbewijstype == "A2") checked @endif @endforeach value="A2">A2</input>
<input type="checkbox" name="rijbewijstype[]" @foreach ($types as $type) @if ($type->rijbewijstype == "A") checked @endif @endforeach value="A">A</input>
<input type="checkbox" name="rijbewijstype[]" @foreach ($types as $type) @if ($type->rijbewijstype == "B1") checked @endif @endforeach value="B1">B1</input>
<input type="checkbox" name="rijbewijstype[]" @foreach ($types as $type) @if ($type->rijbewijstype == "B") checked @endif @endforeach value="B">B</input> <br>
<input type="checkbox" name="rijbewijstype[]" @foreach ($types as $type) @if ($type->rijbewijstype == "C1") checked @endif @endforeach value="C1">C1</input>
<input type="checkbox" name="rijbewijstype[]" @foreach ($types as $type) @if ($type->rijbewijstype == "D1") checked @endif @endforeach value="D1">D1</input>
<input type="checkbox" name="rijbewijstype[]" @foreach ($types as $type) @if($type->rijbewijstype == "D") checked @endif @endforeach value="D">D</input>
<input type="checkbox" name="rijbewijstype[]" @foreach ($types as $type) @if ($type->rijbewijstype == "BE") checked @endif @endforeach value="BE">BE</input>
<input type="checkbox" name="rijbewijstype[]" @foreach ($types as $type) @if ($type->rijbewijstype == "C1E") checked @endif @endforeach value="C1E">C1E</input>
<input type="checkbox" name="rijbewijstype[]" @foreach ($types as $type) @if ($type->rijbewijstype == "CE") checked @endif @endforeach value="CE">CE</input>
<input type="checkbox" name="rijbewijstype[]" @foreach ($types as $type) @if ($type->rijbewijstype == "D1E") checked @endif @endforeach value="D1E">D1E</input>
<input type="checkbox" name="rijbewijstype[]" @foreach ($types as $type) @if ($type->rijbewijstype == "DE") checked @endif @endforeach value="DE">DE</input>
<input type="checkbox" name="rijbewijstype[]" @foreach ($types as $type) @if ($type->rijbewijstype == "T") checked @endif @endforeach value="T">T</input>
</div>
</div>
简短的回答,您没有将"rijbewijsnummer"保存在类型表中。
长答案(使用注释来解释代码及其作用(:
$user = Auth::user();
$type = Type::all();
// Here you set the attribute rijbewijsnummer to the input value.
$user->rijbewijsnummer = Request::input('rijbewijsnummer');
// Here you set the selected type as an attribute on the type object.
$type->rijbewijstype = Request::input('rijbewijstype');
// Here you save the user (this means you can now get the rijbewijsnummer from the user table)
$user->save();
// But you don't save the Type you selected,
// let alone add the rijbewijsnummer to it,
// so even if you did save it, you'd just get an empty rijbewijsnummer
// and rijbewijstype would be filled.
// (or rather, overwrite the other empty rijbewijsnummer field with the new rijbewijstype, if you allow rijbewijsnummer to be empty)
return redirect()->route('home');
您可以通过更改以下行来解决此问题。
$type->rijbewijstype = Request::input('rijbewijstype');
自
// Creates a new entry in Types, using the rijbewijstype and rijbewijsnummer.
$type->create(['rijbewijstype' => Request::input('rijbewijstype'), 'rijbewijsnummer' => Request::input('rijbewijsnummer'));
我假设您希望为不同类型的保存同一 rijbewijsnummer 的多个条目。
您还使用了一些低效的代码,我认为您的数据库结构没有经过深思熟虑。但这超出了这个问题的范围。如果您的代码完全正常工作,您可以随时寻求帮助以改进代码审查堆栈上的代码。