Assign and send unchecked checkbox value in Larevel

Asked

Viewed 281 times

0

I have a form where the user can enable or disable certain options by checking or not 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 your value becoming "active" and when it is cleared it will be "inactive".

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

But I realized that when the checkbox is unchecked it simply is not sent on request, he sends the ppp, the pcmso and ignores the ppra who was feeling inactive.

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

In my controller I do not specify all fields, I simultaneously take everything and create the record.

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

How can I fix this?

inserir a descrição da imagem aqui inserir a descrição da imagem aqui

1 answer

0

Well, I decided to remove the function to assign a value when the checkbox is unchecked and make a change to 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 it does not receive a value it assigns the 0.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.