将数据从<option>插入Laravel中的数据库

时间:2017-09-01 08:44:37

标签: mysql laravel frameworks

尝试在CRUD应用中使用Option表单插入数据。但它说

  

SQLSTATE [23000]:完整性约束违规:1048列'site_category'不能为空(SQL:插入companysite_categorycategory_restuser_id,{ {1}},updated_at)值(,, 1,2017-09-01 08:30:09,2017-09-01 08:30:09))

查看

created_at

控制器

                      {!! Form::open(['action' => 'CompanyController@store', 'method' => 'POST']) !!}
                        <div class="form-group">
                            <label for="siteCategory">Choose your website category</label><br>
                            <select class="custom-select" id="siteCategory">
                                  <option name="test1" value="test1">Option 1</option>
                                  <option name="test2" value="test2">Option 2</option>
                                  <option name="test3" value="test3">Option 3</option>
                            </select>
                        </div>
                         <div class="form-group">
                            <label for="restrictCategory">Choose your website categories restrictions</label><br>
                            <select class="custom-select" id="restrictCategory" multiple>
                                  <option name="test4" value="test4">Option 1</option>
                                  <option name="test5" value="test5">Option 2</option>
                                  <option name="test6" value="test6">Option 3</option>
                            </select>
                        </div>
                        <button type="submit" class="btn btn-primary">Add Company</button>
                    {!! Form::close() !!} 

我觉得我的动作控制器有问题,但是什么?

1 个答案:

答案 0 :(得分:0)

您必须为元素命名,它们代表&#34; $ request&#34;中的键。关联数组。

<select class="custom-select" id="siteCategory" name="siteCategory">

<select class="custom-select" id="restrictCategory" name="restrictCategory[]" multiple> // set name attribute to an array to get all the selected choices

控制器

public function store(Request $request)
{
    $company = new Company;
    $company->site_category = $request->input('siteCategory');
    $company->category_rest= implode(',', $request->input('restrictCategory')); // store it as string separated by commas
    $company->user_id = auth()->user()->id; //sync with user
    $company->save();

    return redirect('/dashboard/company');
}