php Laravel-5 如何使用 id 值和名称值从数据库中填充选择框
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/29508297/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Laravel-5 how to populate select box from database with id value and name value
提问by borracciaBlu
I want to create a select box like the one below using illuminate\html:
我想创建一个像下面这样的选择框使用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.
问题是,而不是$item->name
显示实体的所有信息。
回答by mininoz
Laravel provides a Query Builder with lists() function
Laravel 提供了一个带有 list() 函数的查询生成器
In your case, you can replace your code
在您的情况下,您可以替换您的代码
$items = Items::all(['id', 'name']);
with
和
$items = Items::lists('name', 'id');
Also, you can chain it with other Query Builder as well.
此外,您也可以将它与其他查询生成器链接起来。
$items = Items::where('active', true)->orderBy('name')->lists('name', 'id');
source: http://laravel.com/docs/5.0/queries#selects
来源:http: //laravel.com/docs/5.0/queries#selects
Update for Laravel 5.2
Laravel 5.2 更新
Thank you very much @jarry. As you mentioned, the function for Laravel 5.2 should be
非常感谢@jarry。正如你所提到的,Laravel 5.2 的功能应该是
$items = Items::pluck('name', 'id');
or
或者
$items = Items::where('active', true)->orderBy('name')->pluck('name', 'id');
ref: https://laravel.com/docs/5.2/upgrade#upgrade-5.2.0-- look at Deprecations lists
参考:https: //laravel.com/docs/5.2/upgrade#upgrade-5.2.0——查看弃用列表
回答by lewis4u
Laravel >= 5.3 method lists() is deprecated use pluck()
Laravel >= 5.3 方法lists() 已弃用 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
这将为您提供一个选择框,其选择选项与 DB 中的 ID 号相同
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();
我发现 pluck 现在返回一个集合,你需要在 pluck 的末尾添加 ->toArray() ......就像这样: pluck('name', 'id')->toArray();
回答by Sohel0415
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
希望这能解决你的问题
回答by Hashmat Waziri
Controller
控制器
$campaignStatus = Campaign::lists('status', 'id');
compact('campaignStatus') will result in [id=>status]; //example [1 => 'pending']
compact('campaignStatus') 将导致 [id=>status]; //示例 [1 => 'pending']
return view('management.campaign.index', compact('campaignStatus'));
View
看法
{!! Form::select('status', $campaignStatus, array('class' => 'form-control')) !!}
回答by itzmebibin
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>
回答by Naing Win
Laravel use array for Form::select
. So I passed array like below:
Laravel 使用数组为Form::select
. 所以我传递了如下数组:
$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>
回答by Gaetano
Laravel 5.3 use pluck($value, $key )
Laravel 5.3 使用 pluck($value, $key )
$value is displayed in your drop list and $key is id
$value 显示在您的下拉列表中, $key 是 id
controller
控制器
$products = Product::pluck('name', 'id');
return view('main.index', compact('products'));
view
看法
{{ Form::select('id', $products, null, ['class' => 'form-control']) }}
回答by Dominic Tan
Laravel 5.*
Laravel 5.*
In your controller:
在您的控制器中:
$items= Items::pluck('name', 'id')->toArray();
return view('your view', compact('items', $items));
In your view:
在您看来:
{{ Form::select('organization_id', $items, null, []) }}
回答by Gediminas
I have added toArray()
after pluck
我在toArray()
之后添加了pluck
$items = Item::get()->pluck('name', 'id')->toArray();
{{ Form::select('item_id', [null=>'Please Select'] + $items) }}
回答by Manojkiran.A
Sorry for the late reply
这么晚才回复很抱歉
Obviously lists
method has been deprecated
in Laravel, but you can use the pluck method.
显然Laravel中lists
已经有了deprecated
method,但是可以使用pluck方法。
For Eg:
例如:
Laravel 5.7
Laravel 5.7
public function create()
{
$countries = Country::pluck('country_name','id');
return View::make('test.new')->with('countries', $countries);
}
and in the view if you are FORM
components just pass as
并且在视图中,如果您是FORM
组件,则作为
{{ Form::select('testname',$countries,null,['class' => 'required form-control select2','id'=>'testname']) }}
if will generate the dropdown
if 会生成下拉菜单
but i have a situation to show that select country as the first option and as null value
但我有一种情况表明选择国家作为第一个选项和空值
<option value="" selected="selected">--Select Country--</option>
so I have referred to
所以我提到了
https://stackoverflow.com/a/51324218/8487424
and fixed this, but in later times if I want to change this I hate being changing it in the view
并修复了这个问题,但以后如果我想改变它,我讨厌在视图中改变它
So have created the helper function based on https://stackoverflow.com/a/51324218/8487424
and placed in the Country Model
因此创建了基于https://stackoverflow.com/a/51324218/8487424
Country Model的 helper 函数并将其放置在 Country Model 中
public static function toDropDown($tableName='',$nameField='',$idField='',$defaultNullText='--Select--')
{
if ($idField == null)
{
$idField="id";
}
$listFiledValues = DB::table($tableName)->select($idField,$nameField)->get();
$selectArray=[];
$selectArray[null] = $defaultNullText;
foreach ($listFiledValues as $listFiledValue)
{
$selectArray[$listFiledValue->$idField] = $listFiledValue->$nameField;
}
return $selectArray;
}
and in controller
并在控制器中
public function create()
{
$countries = Country::toDropDown('countries','name','id','--Select Country--');
return View::make('test.new')->with('countries', $countries);
}
and finally in the view
最后在视图中
{{ Form::select('testname',$countries,null,['class' => 'required form-control select2','id'=>'testname']) }}
and the result is as expected, but I strongly recommend to use pluck()
method
结果如预期,但我强烈建议使用pluck()
方法