Well null is not the same as 0. So you can change checkbox value to 0 if null in database.
checkbox still checked with NULL
I'm trying to get my checkbox to be unchecked when NULL is in the db and checked when it is 1 (true), but it seems the checkbox will not uncheck unless I use a 0. Is it possible accomplish something like this using something like 'checked' => 'none'? The reason I ask is because I want to add a constraint so only 1 row can be true at any given time.
{!! Form::hidden('employee', 'NULL') !!}
{!! Form::checkbox('employee', 1) !!}
Form::checkbox() has a method signature of $name[, $value = 1, $checked = null, $options = [] ]. The second parameter determines the value of the value attribute, whilst the third parameter determines whether or not the checkbox is checked, which is where you need to place your boolean check.
You'll probably want something like the following:
{!! Form::checkbox('employee', 1, $existing->employee) !!}
Thanks guys. I think I figured out my problem. It seems specifying 'NULL' in the form field isn't the same as when it's set by phpmyadmin. Or when a new row is inserted which defaults to NULL.
Is it possible to always force a default NULL if the row is 0 or empty.
This doesn't work, because it's not really NULL, it's a string.
{!! Form::hidden('employee', 'NULL') !!}
This doesn't work, because I can't have no more than 1 of the same value.
{!! Form::hidden('employee', 0) !!}
This doesn't work, because it's an empty string.
{!! Form::hidden('employee', '') !!}
Yeah there's no HTML "null". String, integer or empty.
Right, which brings me to the question, is it possible to force the row to revert no null automatically if the field is left empty or if it contains a 0?
Maybe a better way to put it is, is it possible to force every row to be null unless it is a 1?
For a checkbox input, the value you give it is passed only when the checkbox is set (checked).
When you post a form; if the checkbox isn't checked, it won't go in your request. You'll need a $request->get('employee', null)(null might be the default if not set, I don't recall off the top of my head) and use that - or whatever you need as the default value - when you insert/update your model.
@dmcglone This is what I'm currently doing and it works fine.
Just add these lines.
{!! Form::hidden('employee', 0) !!}
{!! Form::checkbox('employee', 1, null) !!}
@deringer, Ok I'll look into it.
@mercuryseries, That is the way I had it once, which does work, but I was hoping to use a unique constraint hoping if a different checkbox was chosen, any row in the db that is true would reset to NULL on submission.
The reason for wanting NULL instead of 0, is because the unique constraint only allows one row to be true(1) or false(0), but many rows can be null.
On the flip side, If I remove the unique constraint, then it allows more than one row to be true (1)
I'm wondering if it would be better to just create a pivot table and just add the employee id to it to identify the corresponding employee in the users table and when it's updated, just overwrite that record. Think that's a good idea?
When I use mass assignment I merge stuff as 0 or null if it's not set. Is that what you need?
if (! $request->somefield) {
$request->merge(['somefield' => null]);
}
@bashy, I'm not sure I understand what you're saying.
Let's say I have a checkbox. If I uncheck the checkbox and submit it, it will put a 0 in the db. What I'm aiming for is instead of a 0, I want the row to be the default NULL .
Keep in mind,
{!! Form::checkbox('employee', "NULL") !!}
The NULL in the code is actually just text and isn't the same as a newly created row that has NULL set as the default and doesn't work the same either.
I've also tried other variations listed in previous comments to no avail.
I hope that makes sense. I'm basically chasing, if you will, that "default NULL"
With MySQL you want to try and not use null columns. Granted that is not always possable. Null in MySQL utilizes memory and space as if the column was at its limit (images a text column, lots of resources for null).
With that said generally speaking in a Boolean column you set the default value with MySQL (0 or 1 depending). I generally set it as 0 by default so if the checkbox isn't checked it defaults to 0. Though on update you still need to check if that input exists if not set explicitly set to 0 otherwise if already set as 1 will stay 1 (will not update properly).
All that said setting a field to null or 1 causes issues with checks etc as your experiencing. Boolean by definition should be true or false, 1 or 0, not null or 1
You default to 0 if it's not in the request array... put the value to 1 for the checkbox so if a user checks it, it will be 1, else 0 (from merging the request on empty).
I understand now.
So from what I gather from all the information in this thread, the only way to accomplish this is to check for any 1's in the db and change them to a 0 before updating a different row to be 1.
So you want to toggle all db rows depending on the value sent in a form?
@bashy yeah. :-)
Couldn't you do something like this?
Model::all()->update(['status' => 1 - $request->somefield]); // 1 - 1 is 0
// then update the row you want with 1.
That just may work. I'll try doing it that way and see what comes of it.
Thanks everyone for giving me a hand on this, took me a minute to be able to get back to this and work it out. Might not be the most efficient way, but hey it's a breakthrough to me.
DB::table('users')
->where('employee', 1)
->update(array('employee' => 0));
$user = User::findOrFail($id);
$user->update($request->all());
Please or to participate in this conversation.