Friday, March 3, 2017

Get Value from Select Option and Radio Option in PHP Laravel 5.3

How can I get value from select option in view and insert it to DB ?

I tried many different way but it's not work.

Here is my code

My Controller

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);

My view

      <form action="admin/posts/add" method="post">
        <input type="hidden" name="_token" value="">
        <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=""></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>

And My result

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

]

I can get value from title and content but Category Id and Status is not :(

How can I fix that.

Thanks



via Nguyên Hoàng

Advertisement