What is the most graceful way of creating a multidimensional form array using the Form::
facade in Laravel 5.1?
I know that I can create form fields using the following syntax:
{!! Form::label('work_type', 'Work Type') !!}
{!! Form::select('work_type', WorkTypes::show(), ['id' => 'work_type']) !!}
{!! Form::label('date', 'Date & Time Worked') !!}
{!! Form::text('date', date('d/m/Y'), ['id' => 'date', 'class' => 'datepicker']) !!}
{!! Form::label('overtime_start_hour', 'Overtime Start Hour') !!}
{!! Form::select('overtime_start_hour', Hours::show(), ['id' => 'overtime_start_hour']) !!}
{!! Form::label('overtime_start_minute', 'Overtime Start Minute') !!}
{!! Form::select('overtime_start_minute', Minutes::show(), ['id' => 'overtime_start_minute']) !!}
However, i'm looking to create a multidimensional form array. I would want the array to look something like this when the form is submitted:
array(
[0] =>
[work_type] => 'My work type value for row 1...',
[date] => '20/03/2015',
[overtime_start_hour] => array(
[0] => 01
[1] => 03
)
[overtime_start_minute] => array(
[0] => 00
[1] => 00
)
[1] =>
[work_type] => 'My work type value for row 2...',
[date] => '21/03/2015',
[overtime_start_hour] => array(
[0] => 09
[1] => 07
)
[overtime_start_minute] => array(
[0] => 30
[1] => 00
)
)
Can anyone help?
Change your selects to multiples, and the names to arrays, as such:
{!! Form::select('overtime_start_hour[]', Hours::show(), null, ['id' => 'overtime_start_hour', 'multiple' => true]) !!}
{!! Form::select('overtime_start_minute[]', Minutes::show(), null, ['id' => 'overtime_start_minute', 'multiple' => true]) !!}
You can then select multiple items from the select boxes, and get the array you're after. Is that how you wanted it to work?
Edit: you could try adding a row array to all the fields when you add a new row, and increment the row ID and form input ID time
{!! Form::label('work_type0', 'Work Type') !!}
{!! Form::select('row[0][work_type]', WorkTypes::show(), null, ['id' => 'work_type0']) !!}
{!! Form::label('date0', 'Date & Time Worked') !!}
{!! Form::text('row[0][date]', date('d/m/Y'), null, ['id' => 'date0', 'class' => 'datepicker']) !!}
{!! Form::label('overtime_start_hour0', 'Overtime Start Hour') !!}
{!! Form::select('row[0][overtime_start_hour]', Hours::show(), null, ['id' => 'overtime_start_hour0']) !!}
{!! Form::label('overtime_start_minute0', 'Overtime Start Minute') !!}
{!! Form::select('row[0][overtime_start_minute]', Minutes::show(), null, ['id' => 'overtime_start_minute0']) !!}
Next row (obviously)...
{!! Form::label('work_type1', 'Work Type') !!}
{!! Form::select('row[1][work_type]', WorkTypes::show(), null, ['id' => 'work_type1']) !!}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.