0

i am building an app with Laravel, and i have users which also have a profile. I am unsure as the best way to go about setting up the database for the user attributes for example

user_ud  |   name  |  gender  |  haircolor
   1         josh       1           3

as you can see from this quick example this is what my table for profiles currently looks like, as the vales are filled in from select boxes.

my question is. is the bes way to then map these vales to a meaningfull value like this.

 id  |  gender
  1      male
  2      female

 id  | hairColor
  1      blonde
  2      brown
  3      black

and so on creating a new table for each attribute type? or is there a better way of doing this?

2 Answers 2

2

I'd use an ENUM for these:

$table->enum('gender', ['male', 'female']);
Sign up to request clarification or add additional context in comments.

3 Comments

just so i get this right, using this is like saying 1 = male, 2 = female and so on?
No, you'd use the actual values male/female. No reason to complicate it with numeric IDs that don't correspond to anything meaningful - $user->gender = 'male' makes a lot more sense in the code than $user->gender = 1.
You can also use numerical keys for enum values if you wish.
0

Is hair color important as an entity? Do you insert colors by themselves and if nobody has red color, do you still need information about it's existence in the database?

With this kind of data, it's usually not the case and prone to over-normalization of the database, which results in additional queries and no benefits.

Storing the color as a string value is perfectly fine in this case. Enum is also ok for the gender. That's unless you are Facebook and need dozens of "gender" options for people who feel like their rights are violated if you don't include their own specific sexual perspective.

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.