在laravel表单中显示下拉值选择列表?

时间:2017-04-17 10:31:35

标签: php laravel-5

这是我的代码:

控制器文件:EmergencyContactsController.php

$surnames = DB::table('salutations')->pluck('name'); 
return view('patient/emergencycontacts', ['salutation' => $surnames]);

刀片文件:patient / emergencycontacts.blade.php

    {!! Form::open(array('route' => 'emergencycontacts_store', 'class' => 'form')) !!}
<div class="form-group">
{!! Form::label('Salutation') !!}
{{ Form::select('role', ['' => 'Select Role'] + $salutation, null, ['class' => 'form-control']) }}
 </div>
<div class="form-group">
{!! Form::label('First Name') !!}
{!! Form::text('firstname', null, array('required', 'class'=>'form-control', 'placeholder'=>'First Name')) !!}
</div>
<div class="form-group">
{!! Form::label('Last Name') !!}
{!! Form::text('lastname', null, array('required', 'class'=>'form-control', 'placeholder'=>'Last Name')) !!}
</div>
<div class="form-group">
{!! Form::label('Relationship') !!}
{{ Form::select('relationship', ['Father', 'Mother', 'Husband','Wife','Son','Daughter','Uncle','Aunty','Other']) }}
</div>
<div class="form-group">
{!! Form::label('Phone') !!}
{!! Form::text('phone', null, array('required', 'class'=>'form-control', 'placeholder'=>'Phone')) !!}
</div>
<div class="form-group">
{!! Form::label('Fax') !!}
{!! Form::text('fax', null, array('class'=>'form-control', 'placeholder'=>'Fax')) !!}
</div>
<div class="form-group">
{!! Form::submit('Save',array('class'=>'btn btn-primary')) !!}
</div>
{{ Form::close() }}

当我转到网址http://localhost:8000/patient/emergency-contacts/create时,它会给我错误:

  

&#34;不支持的操作数类型&#34;

2 个答案:

答案 0 :(得分:2)

你需要改变2件事

在您的控制器中:

$surnames = DB::table('salutations')->pluck('name', 'id')->toArray(); 

所以你得到的数组为[id => 'value']而不仅仅是['value']。在视图中:

{!! Form::select('role', $salutation, null, ['class' => 'form-control']) !!}
{!! Form::select('relationship', ['Father', 'Mother', 'Husband','Wife','Son','Daughter','Uncle','Aunty','Other']) !!}
{!! Form::close() !!}
总是逃避&#39;表单标签,如果没有,HTML将打印在屏幕上,而不是解析。

答案 1 :(得分:1)

请使用视图(&#39; patient.emergencycontacts&#39;,[&#39;称呼&#39; =&gt; $姓氏]);