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

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

问题描述:

Here is my code:

controller file: EmergencyContactsController.php

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

Blade file: 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() }}

When I go to url http://localhost:8000/patient/emergency-contacts/create it gives me error:

"Unsupported operand types"

这是我的代码: p>

控制器文件:EmergencyContactsController.php p>

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

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

  {!!  Form :: open(array('route'=&gt;'emergencycontacts_store','class'=&gt;'form'))!!} 
&lt; div class =“form-group”&gt; 
 {!!  Form :: label('Salutation')!!} 
 {{Form :: select('role',[''=&gt;'Select Role'] + $ salutation,null,['class'=&gt;'  form-control'])}} 
&lt; / div&gt; 
&lt; div class =“form-group”&gt; 
 {!!  Form :: label('First Name')!!} 
 {!!  Form :: text('firstname',null,array('required','class'=&gt;'form-control','placeholder'=&gt;'First Name'))!!} 
&lt; / div&gt;  
&lt; div class =“form-group”&gt; 
 {!!  Form :: label('姓氏')!!} 
 {!!  Form :: text('lastname',null,array('required','class'=&gt;'form-control','placeholder'=&gt;'姓氏'))!!} 
&lt; / div&gt;  
&lt; div class =“form-group”&gt; 
 {!!  Form :: label('Relationship')!!} 
 {{Form :: select('relationship',['Father','Mother','Husband','Wife','Son','Daughter',  '叔叔','阿姨','其他'])}} 
&lt; / div&gt; 
&lt; div class =“form-group”&gt; 
 {!!  Form :: label('Phone')!!} 
 {!!  Form :: text('phone',null,array('required','class'=&gt;'form-control','placeholder'=&gt;'Phone'))!!} 
&lt; / div&gt; \  n&lt; div class =“form-group”&gt; 
 {!!  Form :: label('Fax')!!} 
 {!!  Form :: text('fax',null,array('class'=&gt;'form-control','placeholder'=&gt;'Fax'))!!} 
&lt; / div&gt; 
&lt; div class  = “形式的基团” &GT; \ N {!  Form :: submit('save',array('class'=&gt;'btn btn-primary'))!!} 
&lt; / div&gt; 
 {{Form :: close()}} 
  代码>  pre> 
 
 

当我去网址时 http:// localhost:8000 / patient / emergency-contacts / create 它给了我错误: p>

“不支持的操作数类型” p> blockquote > div>

You need to change 2 things

In your controller:

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

So you get an array as [id => 'value'] and not only ['value']. In the View:

{!! Form::select('role', $salutation, null, ['class' => 'form-control']) !!}
{!! Form::select('relationship', ['Father', 'Mother', 'Husband','Wife','Son','Daughter','Uncle','Aunty','Other']) !!}
{!! Form::close() !!}

Always 'escape' the Form tags, if not, the HTML will be printed on screen, not parsed.

please use view('patient.emergencycontacts', ['salutation' => $surnames]);