【问题标题】:Form arrays with Laravel 5.1使用 Laravel 5.1 形成数组
【发布时间】:2015-11-07 11:33:22
【问题描述】:

在 Laravel 5.1 中使用 Form:: 外观创建多维表单数组最优雅的方式是什么?

我知道我可以使用以下语法创建表单域:

{!! 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']) !!}

但是,我希望创建一个多维表单数组。提交表单时,我希望数组看起来像这样:

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
                                )
)

谁能帮忙?

【问题讨论】:

  • 你获取数据的变量在哪里?你能告诉我吗?
  • 你能澄清一下你的意思吗?抱歉,您指的是哪个变量?我基本上想设置我的 Laravel 表单来输出一个像上面那样的数组。我只是在问我将如何创建这样的东西。
  • 让你得到你想要的输出数组。你能给我在你的代码中获取的数据吗?
  • 我还没有编写那部分代码,我刚刚创建表单。我还没有处理表格...
  • 使用 foreach 先生 @V4n1ll4

标签: php arrays forms laravel laravel-5


【解决方案1】:

将您的选择更改为多个,并将名称更改为数组,如下所示:

{!! 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]) !!}

然后,您可以从选择框中选择多个项目,并获取您想要的数组。这就是您希望它的工作方式吗?

编辑:您可以尝试在添加新行时向所有字段添加行数组,并增加行 ID 和表单输入 ID 时间

{!! 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']) !!}

下一行(显然)...

{!! Form::label('work_type1', 'Work Type') !!}
{!! Form::select('row[1][work_type]', WorkTypes::show(), null, ['id' => 'work_type1']) !!}

【讨论】:

  • 嗨@benJ!谢谢,但不幸的是,这不起作用,因为我需要它。在页面上,用户可以添加新行。上面的字段是这些行的子字段,因此不可能沿着多选的路线走。用户单击添加新行,它会添加一个包含上述字段的新行,这就是它们需要单选的原因。
  • 啊,好吧,我已经编辑了……这就是你的想法吗?
  • 简短说明:Form::select() 需要一个额外的参数 - 默认/预设值 - 我在其中添加了 null :)
猜你喜欢
  • 1970-01-01
  • 2015-11-16
  • 2015-09-26
  • 2016-01-23
  • 1970-01-01
  • 2018-07-25
  • 1970-01-01
  • 2016-02-13
  • 2015-12-20
相关资源
最近更新 更多