简体   繁体   中英

How to fetch dropdown dynamic value using laravel 5.4.6

i have a problem to get value from dropdown using laravel please tell me i am new to the laravel.

Blade code:

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

Please Provide a Name for your Select HTML tag :

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

Name attribute missing in your code.

Hi You are missing the name attribute in <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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM