0

There is a switch button in the form. My purpose of using this switch button is to determine whether it is passive or active. When I submit the submit form, its value is "on". What I want to do is to go to the controllers as "passive" if its value is "active" if it is selected, and write it to the database like that.

Add standard form only, not ajax

HTML Code below

<div class="col-lg-6">
  <span class="switch">
    <label>
      <input name="status" type="checkbox" checked="checked"/>
      <span></span>
    </label>
  </span>
  <span class="form-text text-muted">{{ __('Is it active?') }}</span>
</div>
</div>
2
  • Checkbox value wont pass to controller, if the checkbox is not checked Commented May 23, 2021 at 10:46
  • You can use $model->status = ($request->status) ?? 1 : 0 ; Commented May 23, 2021 at 11:47

2 Answers 2

2

In your Controller should check it in this way

if ($request->status == 'on') {
   here you code
}
Sign up to request clarification or add additional context in comments.

Comments

0

If you just want to know whether the checkbox is checked, then the answer provided by @Aian will work. You can also use has() on the Request object to determine if a property is present:

if ($request->has('status')) {
    // 'status' property present, do something
}

If you want to send the value passive to your controller rather than use the default on value, then you need to provide that on your input:

<input name="status" type="checkbox" checked="checked" value="passive" />

Now if your checkbox is checked, the value of $request->status will be passive rather than on.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.