Seeding a table with boolean value, 1 or 'true'

Published 1 month ago by Gabotronix

Given an user model/table with a boolean value of is_Super_Admin, how should I assign the value? should I use 1 or true? do I have to use commas like this ('true') if so?

public function run()
    {
        DB::table('users')->insert([
            'name' => 'Gabotronix',
            'email' => '[email protected]',
            'password' => bcrypt('perolaperra2203'),
            'is_Super_Admin' => true,
        ]);
    }
tykus
tykus
1 month ago (520,580 XP)

In my opinion, it should be a boolean true (no quotation marks), because it says what it means. If you use 1 then it could be misinterpreted, or at the very least take a moment to understand.

Helmchen

That depends (for me at least).

If you insert directly to the table i would use 1, as that's the value that get's stored anyway.

If you use models instead, i would use true, cause that's the value that is stored in the model.

That's a matter of taste.

petrit
petrit
1 month ago (28,460 XP)

If using 'true' (with quotation marks) it will considered string. While using boolean data type you can use: true, false, 1, 0, '1' or '0'.

Helmchen

'true' would be also true, but please don't do this, cause 'false' !== false. confusing? yes :D

tykus
tykus
1 month ago (520,580 XP)

This is why I always lean towards true and false as boolean values, their intent in unambiguous.

bwrice
bwrice
1 month ago (43,100 XP)

If your database is MySQL you're better off making the column a tinyint column in the migration as that is what MySQL uses for a "boolean" column anyways, and setting the value with either 1 (true), or 0 (false) as that is what MySQL is going to return to you when you try to retrieve data from a "boolean" column anyways.

jlrdw
jlrdw
1 month ago (215,260 XP)

I use tinyint and store a 0 or 1 but let null be okay if not used. But I normally use for a checkbox.

Vilfago

For a Is_super_admin column the default value should be false in all case, not null. It will spare you some bad surprise.

Please sign in or create an account to participate in this conversation.