如何使用laravel 5.4.6

时间:2017-02-18 07:04:36

标签: php laravel

我有一个问题是使用l​​aravel从下拉列表中获取价值请告诉我我是laravel的新手。

刀片代码:

<form method="POST" action="{{url('/add_sub_cat')}}" enctype="multipart/form-data">
    <input type="hidden" name="_token" value="{{ csrf_token() }}">
    <div class="form-group">
    <label for="cat">Category</label>
      <select class="form-control">
        @foreach($categories as $cat)
        <option id="categoryId" value="{{$cat->id}}" selected="selected">{{ $cat->category }}</option>
        @endforeach
      </select>
    </div>
    <div class="form-group">
      <label for="sub">Sub Category</label>
      <input type="text" class="form-control" name="subcategory" id="" {{ old('subcategory') }}>
    </div>

    <div class="form-group">
       <button type="submit" class="btn btn-default">Create</button>
       <a href="{{url('/subcategory')}}" class="btn btn-default">Cancel</a>
    </div>
</form>

3 个答案:

答案 0 :(得分:1)

Try this.

1.In controller get all the categories list like below.

$categories=DB::table('categories')->get();//use table or model name ur wish.

step 2:pass those values to view page like this:

  return view('viewpagename',compact('categories'));

Controlle part is done 

Now go to view blade.php page and then continue like this:



 <select id="category" name="category" class="form-control">
        <option value="">Select Category</option>
         @foreach($categories as $key => $value)
         <option value="{{$value->parent_id}}">{{$value->cat_name}}</option>
         @endforeach
 </select>

答案 1 :(得分:1)

请为您的Select HTML标记提供名称:

对于Ex:

<select class="form-control" name="your-tag-name">
        @foreach($categories as $cat)
        <option id="categoryId" value="{{$cat->id}}" selected="selected">{{ $cat->category }}</option>
        @endforeach
      </select>

代码中缺少名称属性。

答案 2 :(得分:0)

您好{01}}中缺少名称属性。

<form method="POST" action="{{url('/add_sub_cat')}}" enctype="multipart/form-data">
<input type="hidden" name="_token" value="{{ csrf_token() }}">
<div class="form-group">
<label for="cat">Category</label>
  <select class="form-control">
    @foreach($categories as $cat)
    <option id="categoryId" value="{{$cat->id}}" selected="selected">{{ $cat->category }}</option>
    @endforeach
  </select>
</div>
<div class="form-group">
  <label for="sub">Sub Category</label>
  <input type="text" class="form-control" name="subcategory" id="" {{ old('subcategory') }}>
</div>

<div class="form-group">
   <button type="submit" class="btn btn-default">Create</button>
   <a href="{{url('/subcategory')}}" class="btn btn-default">Cancel</a>
</div>