6

there is a bit(1) type column in database table. but its not working as i expected.

problem is

$invitee = new Invitee();
$invitee->name = "name1";
$invitee->email = "[email protected]";
$invitee->isActive = 0;    // "b'0'", '0', false,   are also not working
$invitee->save();

I need to put a zero 0 in the isActive column but its getting value 1 every time when i try to add a record with a 0.

and i found a question in here.. but the answers are not describing the cause of the problem. glad if someone can explain the issue.

3
  • Are you sure you want bit for something like seems to be boolean? Commented Dec 30, 2014 at 10:50
  • yes, cant change the database :( Commented Dec 30, 2014 at 10:50
  • Did you try $invitee->isActive = "\0001";? The BIT in MySQL works differently than bool, so you cannot check for 1 or 2. The BIT(m) values range from 1-64. If you would just insert a 1 it will be padded with zeros, for example, assigning a value of b'101' to a BIT(6) column is, in effect, the same as assigning b'000101'. Commented Dec 30, 2014 at 10:57

1 Answer 1

16

Having bit type field means that you need to use raw values as a workaround whenever you are inserting/updating that field.

That's because PDO by default will bind these values and they will be treated as strings, thus bit will result in 1:

DB::table('table')->insert(['bit_field' => 0]); // inserts 1
DB::table('table')->insert(['bit_field' => DB::raw(0)]); // inserts 0

And I suggest changing it to tinyint if you could.

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

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.