Laravel-5 how to populate select box from database with id value and name value

后端 未结 15 1742
梦如初夏
梦如初夏 2020-11-29 21:15

I want to create a select box like the one below using illuminate\\html :


                        
    
提交评论

  • 2020-11-29 21:39

    Just change your controller to the following:

    public function create()
    {
        $items = Subject::all(['id', 'name']);
        return View::make('your view', compact('items',$items));
    }
    

    And your view to:

    <div class="form-group">
      {!! Form::Label('item', 'Item:') !!}
      <select class="form-control" name="item_id">
        @foreach($items as $item)
          <option value="{{$item->item_id}}">{{$item->id}}</option>
        @endforeach
      </select>
    </div>
    

    Hope this will solve your problem

    0 讨论(0)
  • 2020-11-29 21:46

    Many has been said already but keep in mind that there are a times where u don't want to output all the records from the database into your select input field ..... Key example I have been working on this school management site where I have to output all the noticeboard categories in a select statement. From my controller this is the code I wrote

    Noticeboard:: groupBy()->pluck('category')->get();

    This way u get distinct record as they have been grouped so no repetition of records

    0 讨论(0)
  • 提交回复
    热议问题