Assign and send checkbox value unchecked in Larevel

0

I have a form where the user can activate or deactivate certain options with or without a checkbox .

<input name="ppra" type="checkbox" data-oFF-text="INATIVO" value="1"
    data-off-color="danger" data-size="small" data-on-text="ATIVO"
    data-on-color="success" {{ $cliente->ppra == 'ativo' ? 'checked' : '' }}
    onchange="checkStat(this, 'check-ppra');" id="check-ppra">

I created a function so that when a checkbox is marked its value becomes "active" and when it is unchecked becomes "inactive" .

function checkStat(input, name) {
    if(input.checked == true){
        $("#"+name).val('ativo');
    }else{
        $("#"+name).val('inativo');
    }
}

But I noticed that when checkbox is unchecked it just is not sent in request , it sends ppp , pcmso and ignores ppra that was inactive.

I have already checked and seen that the function that assigns value to active or inactive works correctly.

In my controller I do not specify all the fields, I simply get everything and create the registry.

$this->repository->update($request->all(), $id);

How can I resolve this?

    
asked by anonymous 28.09.2016 / 19:04

1 answer

0

Well, I've decided to remove the function to assign a value when the checkbox is unchecked and making a change in my controller:

$data = $request->all();

if(!$request->get('ppp')){ $data['ppp'] = 0; }
if(!$request->get('ppra')){ $data['ppra'] = 0; }
if(!$request->get('pcmso')){ $data['pcmso'] = 0; }

$this->repository->update($data, $id);

So if he does not receive a value he assigns 0.

    
28.09.2016 / 20:36