3

I'm currently building a Laravel 5.7 app where I have multiple boolean columns that indicate if some facilities are available for a building (model), eg, toilet yes/no. This works fine, but I was wondering what happens when I add more of these boolean columns later when I deploy the app.

Say I add a boolean column 'lights,' I could give it a default value of 0, but not NULL. So now all my existing buildings will say that there are no 'lights' (because the value is 0), where in reality it should be something like 'don't know' or 'undefined' Like a third state.

Should I use ENUM columns with yes/no/undefined instead? What are best practices for this scenario?

2
  • Why not just 0=undefined, 1=no, 2=yes Commented Feb 2, 2019 at 20:37
  • in your case here you have multichoice (many status) for one column, so you should use enum to achieve that because that will be easy for you later to manage them Commented Feb 2, 2019 at 20:37

4 Answers 4

2

What I would do, is create separate table, with object_id, and facility_id. Now, you can have dynamic facilites table, and connect them with object. Connection will only have what it needs, so not every object "light" or something else.

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

Comments

1

You can certainly create them them as nullable()! It is a common practice, IMO.

As far as best practices go, it depends on how your application should be used. Do you want a user to notice that the state has not been selected one way or the other yet? Maybe you are displaying a prompt to configure the ones that are null. On the other hand, it may be safer to assume that the options should default to false in order to be rendered properly. If this is the case, maybe a notification can go out to your users to update this property.

Comments

1

This worked to me

$table->boolean('lights')->nullable();

1 Comment

This will result in a tinyint(1) NULL, because there is no nullable boolean on the database level.
0

Yes Yo are Right this could be a problem some times

But the Boolean CAN BE USED SUCH AS TRUE (OR) FALSE ie) 0 (OR) 1

where in reality it should be something like 'don't know' or 'undefined' Like a third state.

So in this Situation use Can use Enum

For Example Your table will have ups_availablity

Scenario One :

If you want to add NotAvailable by default just pass the value inside default method

$table->enum('ups_availablity', ['Available', 'NotAvailable'])->default('NotAvailable');

Scenario Two:

If you want to add Null add nullable method

$table->enum('ups_availablity', ['Available', 'NotAvailable'])->nullable();

If You have any clarification Kindly Comment Below

Hope its helps

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.