Laravel-5 how to populate select box from database

2019-01-13 03:08发布

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

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

In my controller I tried this:

public function create()
{
    $items = Items::all(['id', 'name']);

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

And in my view this:

<div class="form-group">
    {!! Form::Label('item', 'Item:') !!}
    {!! Form::select('item_id', $items, null, ['class' => 'form-control']) !!}
</div>

The issue is that instead of $item->name is displaying all the info of the entity.

14条回答
Ridiculous、
2楼-- · 2019-01-13 03:38

Laravel use array for Form::select. So I passed array like below:

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

foreach ($datas as $data)
{
    $items[$data->id] = $data->name;
}

return \View::make('your view', compact('items',$items));

In your view:

<div class="form-group">
    {!! Form::label('item', 'Item:') !!}
    {!! Form::select('item_id', $items, null, ['class' => 'form-control']) !!}
</div>
查看更多
做自己的国王
3楼-- · 2019-01-13 03:38

In your controller, add,

public function create()
{
    $items = array(
        'itemlist' =>  DB::table('itemtable')->get()
      );

    return view('prices.create', $items);
}

And in your view, use

<select name="categories" id="categories" class="form-control">
  @foreach($itemlist as $item)
    <option value="{{ $item->id }}">{{ $item->name }}</option>
  @endforeach
</select>

In select box, it will be like this,

<select>
 <option value="1">item1</option>
 <option value="2">item2</option>
 <option value="3">item3</option>
 ...
</select>
查看更多
走好不送
4楼-- · 2019-01-13 03:42

Laravel >= 5.3 method lists() is deprecated use pluck()

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

{!! Form::select('items', $items, null, ) !!}

This will give you a select box with same select options as id numbers in DB

for example if you have this in your DB table:

id name
1  item1
2  item2
3  item3
4  item4

in select box it will be like this

<select>
<option value="1">item1</option>
<option value="2">item2</option>
<option value="3">item3</option>
<option value="4">item4</option>
</select>

I found out that pluck now returns a collection, and you need to add ->toArray() at the end of pluck...so like this: pluck('name', 'id')->toArray();

查看更多
男人必须洒脱
5楼-- · 2019-01-13 03:43

For Laravel 5 :

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

Push an item onto the beginning of the collection.

$items->prepend($value, $key = null);
查看更多
聊天终结者
6楼-- · 2019-01-13 03:43

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

查看更多
Melony?
7楼-- · 2019-01-13 03:46

Controller

 $campaignStatus = Campaign::lists('status', 'id');

compact('campaignStatus') will result in [id=>status]; //example [1 => 'pending']

return view('management.campaign.index', compact('campaignStatus'));

View

{!! Form::select('status', $campaignStatus, array('class' => 'form-control')) !!}
查看更多
登录 后发表回答