Php 如何在laravel中使用选择选项发送多个数据?

Php 如何在laravel中使用选择选项发送多个数据?,php,database,forms,laravel,Php,Database,Forms,Laravel,我想用select选项发送多个数据。示例:理发选项为长或小。当你是男人的时候应该是20分钟,女人应该是30分钟 现在我有一个表单,它的值中有多个数据: 第一个值是名称。第二个是男人的时间,第三个是女人的时间 <div class="radio"> <label> <input type="radio" name="hairstyle" id="optionsRadios2" value="Kort|20|30">Kort <

我想用select选项发送多个数据。示例:理发选项为长或小。当你是男人的时候应该是20分钟,女人应该是30分钟

现在我有一个表单,它的值中有多个数据: 第一个值是名称。第二个是男人的时间,第三个是女人的时间

<div class="radio">
    <label>
        <input type="radio" name="hairstyle" id="optionsRadios2" value="Kort|20|30">Kort
    </label>
</div>

<div class="radio">
    <label>
        <input type="radio" name="haircolor" value="Wit|20|30">Wit
    </label>
</div>
如您所见,我剥离了
|
并将所有内容保存在它自己的变量中。然后我检查是否设置为男性或女性。在此基础上,我添加了男性或女性的时间,并更新了行


这是可行的,但我认为它相当混乱。这是我应该改变的吗?有人能给我举个例子或者解释一下我需要改变什么吗?

首先,你不应该把时间常数从前端发送到后端。您最好在后端的某个地方对它们进行硬编码。在我的示例中,我只是将它们硬编码为同一控制器方法中的数组

从前端,您应该只发送所选发型/发型类型的名称。例如
kort
wit
。我还建议您对这些值使用小写。以下是控制器操作的外观:

public function store(Request $request)
{
    $tasks = Appointment::create($request->all());

    /* Get the values */
    $gender = $request->input('gender');
    $hairstyle = $request->input('hairstyle');
    $haircolor = $request->input('haircolor'); 

    // Predefined constants
    $hairstyle_times = [
        'men' => [
            'kort' => 20,
            'something_else' => 30,
            //...etc
        ],
        'women' => [
            'kort' => 30,
            'something_else' => 40,
            //...etc
        ],

    ];

    $haircolor_times = [
        'men' => [
            'wit' => 20,
            'something_else' => 30,
            //...etc
        ],
        'women' => [
            'wit' => 30,
            'something_else' => 40,
            //...etc
        ]
    ];

    //Calculate the time and save the appointment
    $time = $hairstyle_times[$gender][$hairstyle] + $haircolor_times[$gender][$haircolor];

    /* Update the new values in the row with id .. */
    $titles = DB::table('appointments')
    ->where('id', $tasks->id)
    ->update([
        'hairstyle' => $hairstyle_choice,
        'haircolor' => $haircolor_choice,
        'time_costs' => $time,
    ]);

    return redirect( '/appointments' );

}

这样就很容易定制它。
|
s不再具有魔力。此外,高级用户将无法输入时间值并将其发送到后端。

这更有意义!谢谢你,很高兴我问你。
public function store(Request $request)
{
    $tasks = Appointment::create($request->all());

    /* Get the values */
    $gender = $request->input('gender');
    $hairstyle = $request->input('hairstyle');
    $haircolor = $request->input('haircolor'); 

    // Predefined constants
    $hairstyle_times = [
        'men' => [
            'kort' => 20,
            'something_else' => 30,
            //...etc
        ],
        'women' => [
            'kort' => 30,
            'something_else' => 40,
            //...etc
        ],

    ];

    $haircolor_times = [
        'men' => [
            'wit' => 20,
            'something_else' => 30,
            //...etc
        ],
        'women' => [
            'wit' => 30,
            'something_else' => 40,
            //...etc
        ]
    ];

    //Calculate the time and save the appointment
    $time = $hairstyle_times[$gender][$hairstyle] + $haircolor_times[$gender][$haircolor];

    /* Update the new values in the row with id .. */
    $titles = DB::table('appointments')
    ->where('id', $tasks->id)
    ->update([
        'hairstyle' => $hairstyle_choice,
        'haircolor' => $haircolor_choice,
        'time_costs' => $time,
    ]);

    return redirect( '/appointments' );

}