如何将数据库中的下拉列表中的选定值保存为外键?

时间:2017-12-29 07:56:07

标签: mysql laravel dropdown

我有子类别表,其id是posts表中的外键。子类别将显示在下拉菜单中,在用户选择该选项后,子类别ID应作为外键保存在posts表中。为什么会收到此错误:

  

SQLSTATE [23000]:完整性约束违规:1048列'subcategory_id'不能为空(SQL:插入Posts questiondescriptionsolution,{ {1}},imagetagssubcategory_iduser_idupdated_at)值(nn,nm,nkl,14095989_1298359046904580_8427689213294994369_n.jpg,nlknl ,, 5,2017-12-29 07:32:51,2017-12-29 07:32:51))

Subcategory_id没有通过。怎么解决?

创建():

created_at

商品()

  public function create( Postqs $postqs, Subcategory $sub_cate)
  {
    $sub_cate = DB::table('subcategory')->pluck("sub_cate","id")->all();
      return 
   view('postqs.create',compact('users','categories','sub_cate_id'));
}

}

刀片:

  public function store(Request $request, Subcategory $sub_cate)
   {
    $this->validate($request, [
        'question' => 'required',
        'description' => 'required',
        'tags' => 'required',
    ]);
    $user = auth()->user();
     $data = $request->all();
     $data['user_id']=$user->id;
     $data['subcategory_id']=$sub_cate->id;
  return redirect()->route('postqs.index');

postqs模型:

 <div class="form-group">
   <label for="subcategory_id" class="col-md-2 control label">Category</label>
          <div class="col-md-6">
           <select class="custom-select col-md-10" name="subcategory_id" id="subcategory_id">
            @foreach($categories->sub_cate as $subcategory_id)
               <option value="{{$subcategory_id}}">{{$subcategory_id->sub_cate}}</option>

             @endforeach

     </select>             
            </div>
              </div>

0 个答案:

没有答案