Geoff_S
Geoff_S

Reputation: 5105

Laravel - How do we can get both the value AND the text of a select option?

I have this in my blade

//Blade file   
<select name="rule_type">                                                   
    @foreach($getRuleType as $ruleType)
        <option value="{{$ruleType->promo_rule_typet_id}}">{{ $ruleType->rule_type }}</option>
    @endforeach
</select>

And I'm getting the value of each option in the controller:

// Controller
$form_data = $request->form_data;
parse_str($form_data, $my_array_of_vars);

$rule_type = $my_array_of_vars['rule_type'];

This works perfectly, but I need to get the text of the options as well as the value so they each have their own variable in the controller.

What's the best way to do this?

Upvotes: 1

Views: 58

Answers (1)

Ram Chander
Ram Chander

Reputation: 1518

If you are interested, it may be done the following ways.

// Blade
<select name="rule_type">                                                   
@foreach($getRuleType as $ruleType)
     <option value="{{$ruleType->promo_rule_typet_id}}-{{ $ruleType->rule_type }}">{{ $ruleType->rule_type }}</option>
@endforeach
</select>



// Controller
$rule_type = explode("-", $my_array_of_vars['rule_type']);
//$rule_type[0] will be the $ruleType->promo_rule_typet_id
//$rule_type[1] will be the $ruleType->rule_type

Upvotes: 1

Related Questions