5

I get this error, when I submit the form

SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'onloan' cannot be null

Please help. Sorry for the noob question. I've been searching but the examples are different.

edit.blade.php

<div class="form-group">
  <input type="checkbox" class="" id="onloan" value="1" {{ $item->onloan == 1 ? 'checked' : '' }}>
  <label for="onloan">On Loan</label>
</div>

controller

    public function update(Request $request, Item $item)
    {
        $item->update([
            'name' => $request->name,
            'description' => $request->description,
            'barcode' => $request->barcode,
            'onloan' => $request->onloan //Not Working
        ]);
    }
2
  • add name="onloan" on your input field, otherwise it wont pass data with form submit method. <input type="checkbox" class="" name="onloan" id="onloan" value="1" {{ $item->onloan == 1 ? 'checked' : '' }}> then from your controller 'onloan' => ($request->onloan) ? '1' : '0'; Commented Sep 24, 2020 at 5:17
  • Thanks! I really appreciate it. Sorry my level is too low. I cannot upvote your comment. If you put it in the answer, I can checkmark it. Thanks again! Commented Sep 24, 2020 at 5:20

1 Answer 1

3

add name="onloan" on your input field, otherwise it won't pass data with form submit method.

<input type="checkbox"  class="" name="onloan" id="onloan" value="1" {{ $item->onloan == 1 ? 'checked' : '' }}>

If you uncheck the checkbox, then it will send nothing with submit, so from your controller you can give a value (0) if value is not passed :

'onloan' => ($request->onloan) ? '1' : '0';

Or,

if(isset($request->onloan)) {
  $onloan = "1";
} else {
  $onloan = "0";
}

'onloan' => $onloan,
Sign up to request clarification or add additional context in comments.

1 Comment

Ah. Thank you very much for explaining it. I've been figuring out how it works. This is great!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.