twoam
twoam

Reputation: 892

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?

Upvotes: 1

Views: 1072

Answers (2)

Mr Khan
Mr Khan

Reputation: 1

This is my laravel blade

<select id="dropdown" name="ac" class="form-control" required>
 <option disabled selected value>Select</option>
 <option value="1.50 10" selected>10 for 1.50</option>
 <option value="3.00 20" data-second-value="20">20 for 3.00</option>                               
</select>

And this is how i pass multiple value to laravel request using select

public function connecttransaction(Request $request)
    {                     
        $connectandprice =  $request->input('ac');   
        $values = explode(' ', $connectandprice);
        $price  = $values[0];
        $connects = $values[1];

        Transaction::create([                               
            'amount' =>  $price,            
            'status' =>  'Deposit',           
            'connects' => $connects,             
            'user_id' =>  $authuser,         
        ]);
}

Upvotes: 0

Stalinko
Stalinko

Reputation: 3646

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.

Upvotes: 1

Related Questions