【问题标题】:How to send multiple data with a select option in laravel?如何在 laravel 中使用选择选项发送多个数据?
【发布时间】:2017-01-22 10:27:50
【问题描述】:

我想通过选择选项发送多个数据。示例:理发的选项是长的还是小的。如果你是男人,应该是 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>

在我的store function 中,我剥离数据并根据性别进行更新

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

    /* Get gender value */
    $gender = $request->input('gender');

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

    /* Strip hair options values */
    $hs_values = explode('|', $hairstyle);
    $hairstyle_choice = $hs_values[0];
    $hairstyle_time_men = $hs_values[1];
    $hairstyle_time_woman = $hs_values[2];

    $hc_values = explode('|', $haircolor);
    $haircolor_choice = $hc_values[0];
    $haircolor_time_men = $hc_values[1];
    $haircolor_time_woman = $hc_values[2];

    /* Check if gender = men */            
    if ($gender == 'men'){

        $time = $hairstyle_time_men + $haircolor_time_men;

    }
    /* Woman */
    else{

        $time = $hairstyle_time_woman + $haircolor_time_woman;

    }

    /* 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' );

}

如您所见,我剥离了 | 并将所有内容保存在它自己的变量中。然后我检查它是否设置为男性或女性。基于此,我为男人或女人添加时间并更新行。

这可行,但我认为它很混乱。这是我应该改变的东西吗,有人可以链接我一个例子或解释我需要改变什么吗?

【问题讨论】:

    标签: php database forms laravel


    【解决方案1】:

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

    从前端您应该只发送所选发型/发色类型的名称。例如kortwit。我还建议您对这些值使用小写字母。这是控制器动作的样子:

    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 不再有魔法。此外,高级用户将无法输入他们的时间值并将其发送到后端。

    【讨论】:

    • 这更有意义!谢谢,很高兴我问了。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-11-07
    • 1970-01-01
    • 1970-01-01
    • 2021-12-15
    • 2021-08-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多