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 = "example@mail.com";
$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.

Community
  • 1
  • 1
Kalhan.Toress
  • 21,683
  • 8
  • 68
  • 92
  • Are you sure you want `bit` for something like seems to be `boolean`? – Jarek Tkaczyk Dec 30 '14 at 10:50
  • yes, cant change the database :( – Kalhan.Toress Dec 30 '14 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'. – Adrenaxus Dec 30 '14 at 10:57

1 Answers1

15

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.

Jarek Tkaczyk
  • 78,987
  • 25
  • 159
  • 157