我从类别表中获取了ID和名称,并在下拉列表中查看了所有名称,选择名称后,我想将该名称的ID作为外键存储在产品表中
这是我的功能
public function create()
{
$category = category::pluck('name','id');
return view ('books.addbook',compact('category'));
}
public function store(Request $request)
{
$this->validate($request,[
'category'=>'required',
]);
books::create($request->all());
}
这是视图
<div class="form-group row">
<label for="colFormLabelLg" class="col-form-label col-form-label-lg">category</label>
<select class="col-sm-10" name ="category">
<option ></option>
@foreach($category as $cat)
<option class="col-sm-10" value="{{$cat}}" name ="category" class="form-control form-control-lg" >{{$cat}}</option>
@endforeach
</select>
</div>
</div>
我如何仅存储类别的ID?