I would like to pre-select a value for my dropdown but do not know what to put in order for the equivalence comparison. My current code is below:
{!! Form::select('suggest_organization',Organization::pluck('organization_name', "organization_recordid"),$organization->organization_recordid,['class'=> 'form-control selectpicker','id' => 'suggest_organization','data-live-search' => 'true','data-size' => '5']) !!}
I suppose the space I put $organization->organization_recordid is where I should do the pre-selection. The value that I want to select is {{$service->organizations()->first()->organization_name}}, however, I need to select from the Organization::pluck(‘organization_name’, “organization_recordid”) table with equivalent value. Any ideas on how to implement this?
Appreciate any help!
Advertisement
Answer
you have to add the key to make an option pre selected. in your select, organization_name
is the value and organization_recordid
is the key for options. so just put the service organization_recordid
as the third parameter and it will be pre selected.
{!! Form::select('suggest_organization', Organization::pluck('organization_name', "organization_recordid"), $service->organizations()->first()->organization_recordid ?? '', ['class' => 'form-control selectpicker', 'id' => 'suggest_organization', 'data-live-search' => 'true', 'data-size' => '5']) !!}
here i have added null coalescing operator to check for value existence. it will check if the value exists or put an empty string. you have to have php 7.3+ for using null coalescing operator. if your php version is below 7.3, you can use a ternary if condition.
$service->organizations()->first() ? $service->organizations()->first()->organization_recordid : ''