Laravel-用派生值填充选择列表 [英] Laravel - Populating Select List with Derived Values
问题描述
我有一个雄辩的模型(Presenters),该模型具有三列:id
,first_name
和last_name
.我想做的是填充选择选项,以便id是值,而first_name . last_name
是显示的内容.
I have an Eloquent model (Presenters) that has three columns: id
, first_name
and last_name
. What I want to do is populate the select options so that the id is the value, and the first_name . last_name
is what is displayed.
例如,如果我有这些主持人:
For instance, if I had these presenters:
id, first_name, last_name
1, Billy, Bob
2, Jose, Cuervo
3, Puff, the Magic Dragon
我想要与此输出类似的东西:
I want something similar to this output:
<select name="presenters">
<option value="1">Billy Bob</option>
<option value="2">Jose Cuervo</option>
<option value="3">Puff the Magic Dragon</option>
</select>
我知道,如果我只关心选择列表中的姓氏或名字,我可以这样做:
I know that if I only care about the last name or first name in the select list, I can do this:
{{ Form::select("presenters", Presenter::lists("first_name", "id"), Input::old("presenters"), array( "class" => "form-control" )) }}
关于在Laravel中填充模型的选择输入,已经有许多问题找到了一个显示如何填充选择输入的选项,其中每个选项中都将显示派生值,而不仅仅是单个列.
There are many questions already regarding populating select inputs for models in Laravel, although I haven't found one yet that shows how to populate a select input where a derived value is to be shown in each of the options rather than just a single column.
我可以使用lists
方法填充select元素,还是需要填充自己的值/选项文本数组?
Can I use the lists
method to populate the select element, or will I need to populate my own array of values / option texts?
推荐答案
Presenter::select('id', DB::raw('CONCAT(first_name, " ", last_name) AS full_name'))->lists('full_name', 'id');
这篇关于Laravel-用派生值填充选择列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!