Albert90's avatar

Sqlite 3 and HTML checkbox trouble

I have a controller, wich has got the logic for a form-group. This form-group has two checkboxes and i store the valuer in an Sqlie3 database. (There are booleans represented as TinyInt-s, and these can store the value of 0 and 1.

I try to represent this problem in my controller like this, but it won't set the proper 0 or 1 digits in the database. What am i doing wrong?

The checkbox values are stored in $request->elintezve ans $request->ugyfelnek_kiadva.

public function feluliras (Request $request, $id)
    {
        $update = MainNumbers::find($id);
        $update->foszam = $request->foszam;
        $update->ev = $request->ev;
        $update->elso_szemely = $request->elso_szemely;
        $update->masodik_szemely = $request->masodik_szemely;
        $update->ugytargy = $request->ugytargy;
        $update->berkezett = $request->beerkezett;
        if ($request->elintezve) {
            $update->elintezve = 1;
        } else {
            $update->elintezve = 0;
        }
        if (!$request->ugyfelnek_kiadva) {
            $update->ugyfelnek_kiadva = 1;
        } else {
            $update->ugyfelnek_kiadva = 0;
        }
        $update->save();

        return view('master');
    }

0 likes
6 replies
ouhare's avatar

Can you post the result of:

dd($request->all());
Snapey's avatar

Carefully check the column types and names of the database columns.

also, this can be significantly simplified

        if ($request->elintezve) {
            $update->elintezve = 1;
        } else {
            $update->elintezve = 0;
        }

to

    $update->elintezve=$request->elintezve?1:0;
shez1983's avatar
shez1983
Best Answer
Level 23

you dont need to do if else..

just do: $update->elintezve = $request->elintezve ?? 0

checkbox values as you realise do not get posted if users dont check them.. so there could be something wrong with spelling ie in your controller vs how you called them in your form.. really need to see form code for checkboxes (& as the above said a dd() )

Albert90's avatar

Oh, thank You all for the quick answers, im gonna try these next day and report back the result.

Albert90's avatar

Weel, You were all right, there are two "null" values with the checkboxes checked at the end of the update. Here is the output of dd($request->all()):

array:8 [▼
  "ev" => "2018"
  "foszam" => "1"
  "elso_szemely" => "Aimee Baumbach V"
  "masodik_szemely" => "Brandy Cole"
  "ugytargy" => "Autem quae at voluptas."
  "beerkezett" => "2007-08-22"
  "elintezve" => null
  "ugyfelnek_kiadva" => null
]

And with the unchecked checkboxes there is nothing at all:

array:6 [▼
  "ev" => "2018"
  "foszam" => "1"
  "elso_szemely" => "Aimee Baumbach V"
  "masodik_szemely" => "Brandy Cole"
  "ugytargy" => "Autem quae at voluptas."
  "beerkezett" => "2007-08-22"
]

Albert90's avatar

Shez was right. The "value" property of the "input" tag was empty in my html form, that caused the missing variable in the array. (The $request array contained a 'null' if the cbox was checked.)

Thank You very much!

1 like

Please or to participate in this conversation.