Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

mattnewark's avatar

Flagged Enum

Hi Everyone,

I am trying to find the best way of having days in a column in the DB, at present for ease we have created 7 columns Monday, Tuesday etc that either have a 1 or a 0, but I was wondering if there is a better way in Laravel that would only have 1 column with all the details like the C# flagged Enum.

Thanks

0 likes
5 replies
Talinon's avatar

If you just need to hold 1 day within the field, you can create an ENUM column within your migration:


$table->enum('days_of_week', ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday','Saturday','Sunday']);


If you want to be able to include multiple days in one field, you'll likely need to create some of your own bitwise operations.

Cronix's avatar

I just use the built in day of week functions of mysql and store the day in the db as an integer. 0 = Sunday, 6 = Saturday, which is also the same scheme that php assigns numbers to days of the week (w flag in date()). This just makes it easier to use php/mysql's built in date functions and translating, like back to the textual representation "Sunday", etc.

https://dev.mysql.com/doc/refman/5.7/en/date-and-time-functions.html http://php.net/manual/en/function.date.php

I'd avoid enum datatype. There are many drawbacks that you will find if you google them. It might be ok for this specific case since you will never add an additional day of the week (there will always be 7), but generally enums in the database are a bad choice because you can't just add or remove a value without having to manually change all of the values. What if you need to display the days in a different language? With enum you can't, the values are hard-coded.

1 like
mattnewark's avatar

Hi @Cronix

So are you saying that in the table you would have numbers from 0-6 so if it was Monday and Friday you would have 1 and 5? And so on? And then when you call those numbers back you split them and get the assigned days?

Thanks

bensampo's avatar

I wrote a package which simulates enums but actually uses class constants.

https://github.com/BenSampo/laravel-enum

You could set up an enum like this:

// DaysEnum.php
namespace DummyNamespace;

use BenSampo\Enum\Enum;

final class Days extends Enum
{
    const Sunday = 0;
    const Monday = 1;
    const Tuesday = 2;
    const Wednesday = 3;
    const Thursday = 4;
    const Friday = 5;
    const Saturday = 6;
}

Your type in the DB column would be an int and you'd store the values 0-6 depending on the day.

You could then use the enum like this:

if ($day == Days::Tuesday) {
    // Do Tuesday stuff
}

Please or to participate in this conversation.