Search code examples
phpdatabaseformslaravel

How to send multiple data with a select option in laravel?


I want to send multiple data with a select option. Example : The option for a haircut is long or small. When you are a men it should be 20min and for woman 30min.

Right now I have a form with multiple data in the value: The first value is the name. The second is the time for the men and third for the woman.

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

In my store function I strip the data and update it based on the gender

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

}

As you can see I strip the | and save everything in it's own variable. Then I check if it's set to men or woman. Based on that I add the time for man or woman and update the row.

This works but I think it's pretty messy. Is this something I should change, can someone link me a example or explain what I need to change?


Solution

  • First of all you shouldn't send time constants from frontend to backend. You should better hardcode them somewhere on backend. In my example I just hardcode them as arrays in same controller method.

    From the frontend you should send only names of chosen hairstyle/haircolor types. For example kort or wit. I also recommend you using lowercase for these values. This is how the controller action can look like:

    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' );
    
    }
    

    This way it's very easy to customize it. There is no magic with |s anymore. Also advanced users won't be able to put their time values and send them to the backend.