Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel 4.2 BIT datatype issue

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.

like image 623
Kalhan.Toress Avatar asked Dec 30 '14 10:12

Kalhan.Toress


1 Answers

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.

like image 137
Jarek Tkaczyk Avatar answered Nov 02 '22 08:11

Jarek Tkaczyk