从PHP Laravel 5.3中的Select Option和Radio Option获取值

时间:2017-03-04 03:27:41

标签: php laravel

如何从视图中的select选项中获取值并将其插入到DB中?

我尝试了许多不同的方法,但这不起作用。

这是我的代码

我的控制器

public function getAdd(){
      $categories = Categories::all();
      return view('admin.posts.add',['categories'=>$categories]);
    }

    public function postAdd(Request $request){
      $posts = new Posts();

      $posts->title = $request->title;
      $posts->content = $request->content;
      $posts->category_id = $request->category_id;
      $posts->status = $request->status;
      dd($posts);

我的观点

      <form action="admin/posts/add" method="post">
        <input type="hidden" name="_token" value="{{ csrf_token() }}">
        <div class="form-group">
          <label>Tiêu Đề</label>
          <input class="form-control" name="title" placeholder="Tên Tiêu Đề">
        </div>
        <div class="form-group">
          <label>Nội Dung</label>
          <textarea class="form-control ckeditor" name="content" rows="10"></textarea>
        </div>
        <div class="form-group">
          <label>Chọn Danh Mục</label>
          <select nane = "category_id" class="form-control">
            <?php foreach ($categories as $cate): ?>
              <option value="{{$cate->id}}">{{$cate->name}}</option>
            <?php endforeach; ?>
          </select>
        </div>
        <div class="form-group">
          <label>Trạng Thái</label>
          <label class="radio-inline">
            <input type="radio" name="optionsRadiosInline" name="status" posts id="optionsRadiosInline1" value="1" checked="">Xuất Bản
          </label>
          <label class="radio-inline">
            <input type="radio" name="optionsRadiosInline" name="status" posts id="optionsRadiosInline2" value="0">Nháp
          </label>
        </div>
        <button type="submit"class="btn btn-default">Submit Button</button>
        <button type="reset" class="btn btn-default">Reset Button</button>
      </form>

我的结果

#attributes: array:4 [▼
"title" => "sdfsfd"
"content" => """
  <p>\r\n
  \tsfsdf</p>
  """
"category_id" => null
"status" => null

我可以从标题和内容中获取价值,但类别ID和状态不是:(

我该如何解决这个问题。

由于

0 个答案:

没有答案