Emiliano
Emiliano

Reputation: 141

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

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()}}

Upvotes: 4

Views: 7513

Answers (2)

olgundutkan
olgundutkan

Reputation: 406

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.

Upvotes: 7

kipzes
kipzes

Reputation: 714

Or you can pass the categories from your controller like this:

$categories = Category::all();

And inside your blade generate the dropdown like this:

{!! Form::select('category', $categories->lists('name', 'id'), Input::old('category'), ['class' => 'form-control']) !!}

Upvotes: 1

Related Questions