Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel: How get the value of a select (Drop-Down Lists) to bind with the model?

Tags:

php

laravel

something like... in the view

{{Form::open(array('url'=>'expense/add', 'method' => 'POST', 'class' => 'form-signin'), array('role'=>'form'))}}
      <select id="expense_category_id" class="form-control">
      @foreach($data['categories'] as $category)
        <option value="{{$category->id}}">{{$category->name}}</option>
      @endforeach
    {{Form::submit('submit', array('class'=>'btn btn-lg btn-primary btn-block'))}}
  {{Form::close()}}
like image 711
Emiliano Avatar asked Feb 13 '23 16:02

Emiliano


1 Answers

Controller:

$data['categories'] = Category::lists('name', 'id');

If you are using the attribute Category model controller:

$data['categories'] = Category::get()->lists('name', 'id');

view:

{{ Form::select('expense_category_id', $data['categories'], null, array('class' => 'form-control') }}

For laravel5.3 use pluck.

$data['categories'] = Category::all()->pluck('name', 'id'); Reference

You can try.

like image 149
olgundutkan Avatar answered Feb 16 '23 12:02

olgundutkan