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

时间:2016-05-11 16:12:44

标签: php laravel

我使用的是Laravel 5.2 我的问题是:
如何接收一组复选框并将其保存到数据库中? 例如:

    {!! 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() !!}

我知道如何接收文本,电子邮件,选择,广播,文本区等,并将它们保存到数据库中,但我不知道如何接收一组复选框并将其保存到数据库中。

1 个答案:

答案 0 :(得分:1)

我相信你想要所有复选框的状态。

{!! 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() !!}

现在,如果你检查输入,你将获得

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