Gaurav Sharna
Gaurav Sharna

Reputation: 65

Showing dropdown value in laravel form select List?

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"

Upvotes: 0

Views: 1079

Answers (2)

prashant
prashant

Reputation: 21

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

Upvotes: 1

Dimitri Mostrey
Dimitri Mostrey

Reputation: 2355

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.

Upvotes: 2

Related Questions