我要更新所选的下拉值并将其保存到数据库
在此代码中,我无法从数据库更新下拉列表值
我的控制器代码
for(String[] strArr : manyWords){
for(String str : strArr){
System.out.println(str.substring(0,1);}}
我的查看代码
public function edit($product_id)
{
$all_categories = DB::table('tbl_category')->get();
$all_manufactures = DB::table('tbl_manufacture')->get();
$product = DB::table('tbl_product')
->where('product_id',$product_id)
->get()->first();
return View('admin.edit-product')->with([
'product'=>$product,
'all_categories'=>$all_categories,
'all_manufactures'=>$all_manufactures,
]);
}
答案 0 :(得分:1)
编辑方法显示用于编辑的表单。
/**
* Show the form for editing the specified resource.
*
* @param \App\User $user
* @return \Illuminate\Http\Response
*/
public function edit(User $user)
{
$roles = Role::all();
return view('admin.users.form', [
'roles' => $roles,
'user' => $user
]);
}
您需要更新方法
/**
* Update the specified resource in storage.
*
* @param \Illuminate\Http\Request $request
* @param \App\User $user
* @return \Illuminate\Http\Response
*/
public function update(Request $request, $id)
{
$user = User::find($id);
$user->update($request->all());
$role = Role::findOrFail($request->roles);
$user->syncRoles([$role->id]);
return redirect()->route('users.index');
}
查看
<form action="{{route('users.update', $user->id)}}" method="post">
@csrf
@method('PUT')
<select class="custom-select" name="roles">
@if ($roles)
@foreach($roles as $role)
<option {{ ((old('roles') == $role->id) or (isset($user) and $user->roles->pluck('id')->contains($role->id)) ) ? 'selected' : null }} value="{{$role->id}}">{{$role->display_name}}</option>
@endforeach
@endif
</select>
</form>