2017-04-17 43 views
0

這裏是我的代碼:在laravel窗體中選擇列表中的Showin下拉值?

控制器文件:EmergencyContactsController.php

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

刃文件:病人/ 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() }} 

當我去URL http://localhost:8000/patient/emergency-contacts/create它給我錯誤:

"Unsupported operand types"

回答

2

Yo u需要改變兩件事情

在你的控制器:

$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() !!} 

始終「逃離」的表單標籤,如果沒有,HTML會在屏幕上進行打印,而不是解析。

1

請使用view('patient.emergencycontacts',['salutation'=> $ surnames]);