2017-02-18 24 views
0

我有一個問題,使用laravel從下拉列表中獲取值請告訴我,我是laravel的新手。如何使用laravel獲取下拉動態值5.4.6

刀片代碼:

<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> 
+0

你已經寫在控制器?? – Sona

+0

是的,我已經寫了 – Savvy

+0

檢查我的回答只有一次 – Sona

回答

0

嗨你缺少的name屬性中<select name="anythingYouWant">

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

+0

得到它,@AdnanMumtaz,非常感謝你很多救了我的一天:) – Savvy

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> 
+0

感謝幫助 – Savvy

+0

是它做.. @ MuhammadAmir – Sona

+0

是做了,謝謝@Sona – Savvy

1

請您選擇HTML標籤提供一個名稱:

對於防爆:

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

名稱屬性在你的代碼失蹤。

+0

謝謝@Muthu,我做到了:) – Savvy