doulan9287 2017-04-17 10:31
浏览 78
已采纳

在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"

  • 写回答

2条回答 默认 最新

  • douzhi0107 2017-04-17 13:26
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料