Reputation: 1998
How would i add a data attribute to a laravel {{Form::select}}
array?
For example, I want each of my options to look like this
<option value="1" data-days="182">6 Months</option>
<option value="2" data-days="365">1 Year</option>
How do I add the data attribute in the following select array?
Days Table
id | days | name
1 | 182 | 6 Months
2 | 365 | 1 Year
View
{{Form::select('amount_of_days', $days, null, ['placeholder' => 'Amount of days'])}}
Upvotes: 1
Views: 3864
Reputation: 3240
The best way to set the custom value to the form::select is by using the attribute parameter of form::select function.
controller.php
$contact_options = Contact::all();
$contact_attributes = $contact_options->mapWithKeys(function ($item) {
return [$item->id => ['data-otherInfo' => $item->otherInfo]];
})->toArray();
$contact_options = $contact_options->pluck('name', 'id')->toArray();
blade.php
{!! Form::select('contact_id', $contact_options, old('contact_id', null), ['class'=>"form-control", 'id'=>"contact_id", 'required' => 'required'], $contact_attributes) !!}
Upvotes: 2
Reputation: 2328
For this requiment you need to create the <option>
with loop and simply html
<select name="amount_of_days" placeholder="Amount of days">
@foreach($days as $val)
<option value="{{ $val['id'] }}" data-days="{{ $val['days'] }}"> {{ $val['name'] }}<option>
@endforeach
</select>
Upvotes: 0