Populate values to select box in Laravel

When you want to populate a select box in Laravel it might look a little difficult if you are used with PHP coding but it’s actually much effortless compared to plain PHP coding. Use illuminate\html.

Then add the below code for displaying select box In view:

<select>

    <option value="$item->id">$item->name</option>

    <option value="$item->id">$item->name</option>

</select>

Add the below code in the controller to get data from database and populate to view:

public function create()

{

    $items = Items::lists('name', 'id');

    return view('prices.create', compact('id', 'items'));

}

<div class="form-group">

    {!! Form::Label('item', 'Item:') !!}

    {!! Form::select('item_id', $items, null, ['class' => 'form-control']) !!}

</div>

For Laravel 5.2 use pluck

$items = Items::pluck('name', 'id');