简体   繁体   English

Laravel 5.2:如何接收一组复选框并将其保存到数据库中?

[英]Laravel 5.2: How to receive a group of checkbox and save it into a database?

I am using Laravel 5.2. 我正在使用Laravel 5.2。
My question is: 我的问题是:
How to receive a group of checkbox and save it into a database? 如何接收一组复选框并将其保存到数据库中?
for example: 例如:

    {!! Form::open(array('url' => 'foo/bar')) !!}

  <fieldset class="form-group">
    <label for="exampleInputEmail1">Email address</label>
    <input type="email" class="form-control" id="exampleInputEmail1" placeholder="Enter email">
    <small class="text-muted">We'll never share your email with anyone else.</small>
  </fieldset>
  <fieldset class="form-group">
    <label for="exampleInputPassword1">Password</label>
    <input type="password" class="form-control" id="exampleInputPassword1" placeholder="Password">
  </fieldset>
  <fieldset class="form-group">
    <label for="exampleSelect1">Example select</label>
    <select class="form-control" id="exampleSelect1">
      <option>1</option>
      <option>2</option>
      <option>3</option>
      <option>4</option>
      <option>5</option>
    </select>
  </fieldset>
  <fieldset class="form-group">
    <label for="exampleSelect2">Example multiple select</label>
    <select multiple class="form-control" id="exampleSelect2">
      <option>1</option>
      <option>2</option>
      <option>3</option>
      <option>4</option>
      <option>5</option>
    </select>
  </fieldset>
  <fieldset class="form-group">
    <label for="exampleTextarea">Example textarea</label>
    <textarea class="form-control" id="exampleTextarea" rows="3"></textarea>
  </fieldset>
  <fieldset class="form-group">
    <label for="exampleInputFile">File input</label>
    <input type="file" class="form-control-file" id="exampleInputFile">
    <small class="text-muted">This is some placeholder block-level help text for the above input. It's a bit lighter and easily wraps to a new line.</small>
  </fieldset>
  <div class="radio">
    <label>
      <input type="radio" name="optionsRadios" id="optionsRadios1" value="option1" checked>
      Option one is this and that&mdash;be sure to include why it's great
    </label>
  </div>
  <div class="radio">
    <label>
      <input type="radio" name="optionsRadios" id="optionsRadios2" value="option2">
      Option two can be something else and selecting it will deselect option one
    </label>
  </div>
  <div class="radio disabled">
    <label>
      <input type="radio" name="optionsRadios" id="optionsRadios3" value="option3" disabled>
      Option three is disabled
    </label>
  </div>
  <div class="checkbox">
        <label class="checkbox-inline">
      <input type="checkbox" id="inlineCheckbox1" name="checkbox[]" value="option1"> 1
    </label>
    <label class="checkbox-inline">
      <input type="checkbox" id="inlineCheckbox2" name="checkbox[]" value="option2"> 2
    </label>
    <label class="checkbox-inline">
      <input type="checkbox" id="inlineCheckbox3" name="checkbox[]" value="option3"> 3
    </label>
  </div>

<button type="submit" class="btn btn-primary">Submit</button>
      {!! Form::close() !!}

I know how to receive text,email, select, radio, textarea etc and save them into a database,but I don't know how to receive a group of checkbox and save it into a database. 我知道如何接收文本,电子邮件,选择,广播,文本区域等并将其保存到数据库中,但是我不知道如何接收一组复选框并将其保存到数据库中。

I believe you want the state of all checkboxes. 我相信您想要所有复选框的状态。

{!! Form::open(array('url' => 'foo/bar')) !!}
<label class="checkbox-inline">
  <input type="checkbox" id="inlineCheckbox1" name="checkbox[id1]" value="option1"> 1
</label>
<label class="checkbox-inline">
  <input type="checkbox" id="inlineCheckbox2" name="checkbox[id2]" value="option2"> 2
</label>
<label class="checkbox-inline">
  <input type="checkbox" id="inlineCheckbox3" name="checkbox[id3]" value="option3"> 3
</label>
{!! Form::close() !!}

Now if you check Input, you will get 现在,如果选中输入,您将得到

[
    "checkbox" => [
        "id1" => on/off/Not Set
        "id2" => on/off/Not Set
        "id3" => on/off/Not Set
    ]
]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM