Home > OS >  Checkbox with 2 values Laravel
Checkbox with 2 values Laravel

Time:08-29

i want to make checkbox with 2 values - One with "Yes" and other with "No" and send to database. I try to make it when is not send "Yes" to give me "No" but now dont work. I would be grateful if you could tell me how to do it and what are the errors in my code

<form method="post" action="/adminpanel/hofswitch">
    @csrf
    <div >
        @foreach($char as $value)

        <div >
        @if($value->status == "Yes")
            <input type="hidden" name="id[]" value="{{$value->id}}">
            <input type="checkbox" name="switch[]" value="Yes" checked data-bootstrap-switch data-off-color="danger" data-on-color="success">

            <div >
                <label  for="inputSuccess"><i ></i> Character Class</label>
                <input type="text" name="class" value="{{$value->class}}"  id="inputSuccess" readonly="true" placeholder="{{$value->class}}">
            </div>
            @else
                <input type="hidden" name="id[]" value="{{$value->id}}">
                <input type="checkbox" name="switch[]" value="Yes" data-bootstrap-switch data-off-color="danger" data-on-color="success">

                <div >
                    <label  for="inputError"><i ></i> Character Class</label>
                    <input type="text" name="class" value="{{$value->class}}"  id="inputError" readonly="true" placeholder="{{$value->class}}">
                </div>
            @endif
        </div>

    @endforeach
    <!-- /.card-body -->
    <div >
        <button type="submit" >Submit</button>
    </div>
</form>

And controller

public function hof_switch(Request $request)
{
    foreach ($request->id as $i => $id) {
         $switch = $request->switch;
            if ($request->switch == true) {
                $switch[$i] = "Yes";
            } else {
                $switch[$i] = "No";
            }

        $update = DB::connection('XXX')->table('XXX_HOF')
            ->where('class', $request->class[$i])
            ->update(
                [
                    'status' => $switch[$i],
                ]);
        
    }

    return redirect()->back()->withSuccess('You have switch this class successfully!');
}

CodePudding user response:

The value that which checkbox sends to the controller is whether true or false and you need to check if it's true then use 'yes' else uses 'no':

$switch = $request->switch[$i];
if ($switch == true) {
    $switch = "Yes";
} else {
    $switch = "No";
}

your code has another problem the switch you used here contains a yes or no value and it can't be used as an array:

// value
$switch = $request->switch;

// Accessing as Array
if ($request->switch == true) {
    $switch[$i] = "Yes";
} else {
    $switch[$i] = "No";
}

and this causes the problem.

try to use this:

public function hof_switch(Request $request) {
    $count = count(collect($request->get('id')))

    for($i = 0; $i < $count; $i  ) {
        $switch = $request->switch[$i];

        if ($switch == true) {
            $switch = "Yes";
        } else {
            $switch = "No";
        }

        $update = DB::connection('XXX')->table('XXX_HOF')
            ->where('id', $request->id[$i])
            ->update(
                [
                    'status' => $switch,
                ]);
    }
    return redirect()->back()->withSuccess('You have switch this class successfully!');
}

CodePudding user response:

The functionality you're describing is already baked into the checkbox input type.

If the checkbox is checked, it will be sent as part of the form submission and available in the $request object and its value can be considered truthy (yes). If the checkbox is not checked it will not be sent as part of the form submission and therefore not in the $request object and so can be considered falsey (no).

Update

You're making things more complicated than they need to be, make the value of your checkbox your $value->id. Then you can inspect the $request object to determine if that checkbox was checked.

In your blade file:

<input type="checkbox" name="switch[]" value="{{ $value->id }}" />

In your controller file:

public function hof_switch(Request $request)
{
    if ($request->has('switch')) {
        foreach ($request->switch as $checked) {

            // do something ...
            // $checked will be the value of the checkbox, so $value->id

        }
    }
}
  • Related