1

My blade template contains a form where data can be inserted like name, mail, etc. One input field is a toggle checkbox where you can check whether you are an intern or not.

  • Intern => toggle checked and "Yes" is visible (equals in database 1)
  • Not intern => toggle is not checked and "No" is visible (equals in database 0)

The checking of the box is working but the status intern or extern isn't sent to the database. Bellow, you will find my code. I don't know if this is the correct way to this.

<input checked data-toggle="toggle" data-on="Yes" {{$person->intern_extern == '1' ? 'checked' : ''}}
data-off="No" {{$person->intern_extern == '0' ? 'checked' : ''}} data-onstyle="primary" 
data-offstyle="info" type="checkbox" 
name="intern_extern">

4 Answers 4

3

As discuss with @N69S , I will suggest you a solution that will require less code.

  1. Your yes/no on the client-side equal 1/0 on server-side.
  2. By default, checkbox into form is not send to server-side if it's not checked.

So, using how request work within Laravel, you can create a checkbox like this one:

<input name="intern_extern" type="checkbox" {{$person->intern_extern == 0 ? 'checked' : ''}}>

Into your controller, when updating a person model, do this:

$person->intern_extern = $request->input('intern_extern', 0);

This is what N69S explain about the $request->input():

yes, by default it is null but you can set it like that when you recover the input trait InteractsWithInput @ public function input($key = null, $default = null)

In this case, if the checkbox inter_person is checked, the form will post the input with the value true. If not checked, it will not be send and the value will be 0.

In case you are using this to create your model: $model = Model::create($request->all());, you will have to set the default value into your migration $table->boolean('intern_extern')->default(0); to make all this work.

EDIT - Simple suggestion

When I use boolean, I like to name them like "isSomething". In your case, isIntern or isExtern. So when you have to refer to it, it's simple to read it. isIntern (yes or no) or isExtern (yes/no).

Sign up to request clarification or add additional context in comments.

Comments

1

Try it.

<input  data-toggle="toggle" data-on="Yes" data-off="No" @if(!empty($person) && $person->intern_extern) {{ 'checked' }} @endif data-onstyle="primary" data-offstyle="info" type="checkbox" name="intern_extern">

here @if(!empty($person) && $person->intern_extern) it'll check $person->intern_extern == 1;

Comments

1

You need to have value="Yes" for this to work which is currently absent from your code.

<input
    name="intern_extern"
    type="checkbox" 
    value="Yes"
    {{ $person->intern_extern == '1' ? 'checked' : '' }}
    data-toggle="toggle"
    data-on="Yes"
    data-off="No" {{$person->intern_extern == '0' ? 'checked' : ''}} data-onstyle="primary" 
    data-offstyle="info" 
>

BUT, if my memory serves me correctly, you cannot have a "No" value sent to the backend when submitting the form unless you're using some JS to handle this.

Why? Because checkboxes can only really represent one value, in your example if it's checked it will show up as intern_extern = Yes in the request, however, if the checkbox is unchecked it will be absent from the request.

Considering this you may want to switch to a different form control: <select> for example to represent Yes/No. Or continue using the checkbox but just handle it appropriately in PHP code, i.e. if it's not in the request then fill out the DB with No/0

2 Comments

You still can "use" the checkbox with "No" value, check my answer
@N69S interesting approach thanks for sharing, personally not too keen on it though!
1

A checkbox input is not sent with the form submission if not checked. You can counter it like this:

<input type="hidden" name="intern_extern" value="0"/>
<input checked data-toggle="toggle" data-on="Yes" @if(isset($person->intern_extern) && $person->intern_extern)checked="checked"@endif 
data-onstyle="primary" data-offstyle="info" type="checkbox" value="1" name="intern_extern">

if the checkbox is checked, it will override the hidden input value since it has the same name

6 Comments

You'd need to update the second <input> with value="Yes"
When you recieve this on the back end, will you need to check for Yes/No value and convert them to true/false || 1/0? Because if so, is not more clean to simply use a checkbox normally and make a rule that look if the value exist or not into your controller? isset($request()['intern_person']) ? $person->intern_person = 1 : $person->intern_person = 0;
or simpler $request->input('intern_person', 0)
Is the second parameter act like a default value if the inter_person input is not found?
@ElieMorin yes, by default it is null but you can set it like that when you recover the input trait InteractsWithInput @ public function input($key = null, $default = null)
|

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.