2017-09-01 65 views
0

试图在CRUD应用程序中使用Option窗体插入数据。但其表示,将数据从<option>插入到Laravel的数据库中

SQLSTATE [23000]:完整性约束违规:1048列 'site_category' 不能为空(SQL:插入companysite_categorycategory_restuser_idupdated_atcreated_at)值(,,1,2017年-09-01 8时30分09秒,2017年9月1日8时30分09秒))

查看

     {!! Form::open(['action' => '[email protected]', '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() !!} 

控制器

public function store(Request $request) 
{ 
    $company = new Company; 
    $company->site_category = $request->input('test1'); 
    $company->site_category = $request->input('test2'); 
    $company->site_category = $request->input('test3'); 
    $company->category_rest= $request->input('test4'); 
    $company->category_rest= $request->input('test5'); 
    $company->category_rest= $request->input('test6'); 
    $company->user_id = auth()->user()->id; //sync with user 
    $company->save(); 

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

我想我的行动控制器错了,但什么?

回答

0

您必须为您的元素命名,它们表示“$ request”关联数组中的键。

<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'); 
} 
+0

尼斯,它的工作原理,感谢帮助 顺便说一下,当我选择乘行节省了只有一个值。 可以保存乘法吗? – qqmydarling

+0

是的,你可以。我用一个例子来编辑我的答案,它将数据存储为以逗号分隔的字符串。 –

+0

谢谢你队友;) – qqmydarling

相关问题