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

amiter00's avatar

Schema / Database structure

I'm very new to all of this. Let's say I want to make a site for various chefs worldwide(potentially a massive collection of data). On my index page, it would list individual "posts" displaying the chef and either 2 or 3 of their specialty dishes.

chefstable [id, chef_name]

dishtable [id, chef_id(foreign key), dish1, dish2, dish3(nullable)]

Upon clicking on this link, a new page will load with a more detailed view, including the above + ingredients for each dish and individual cost for each ingredient.

ingredientstable [id, dish_id(foreign key), ingredient1, ingredient2, ingredient3, ingredient4...(nullable), ingredient1cost, ingredient2cost, ingredient3cost...(nullable)]

Would it be more feasible to consolidate some of this data into one table, or are they ok separated into different tables that I will link together. Does anyone have ideas on a better way? I am worried about the structure because there will be a lot of null values (dishes will have varying amounts of ingredients). Any guidance would be much appreciated.

0 likes
4 replies
mstnorris's avatar
Level 55

@amiter00 if anything you've "consolidated" too much already. You'd need to do what is called "normalisation" on your data. Your tables should look like this:

chefs table

| id | name    |
|----|---------|
| 1  | andy    |
| 2  | brian   |
| 3  | charlie |

dishes table

| id | chef_id |
|----|---------|
| 1  | 1       |
| 2  | 1       |
| 3  | 1       |
| 4  | 1       |
| 5  | 2       |
| 6  | 2       |
| 7  | 2       |
| 8  | 2       |
| 9  | 3       |
| 10 | 3       |
| 11 | 3       |
| 12 | 3       |
| 13 | 7       |
| 14 | 7       |

ingedients table

| id | name     | cost |
|----|----------|------|
| 1  | potatoes | 0.3  |
| 2  | tomatoes | 0.4  |
| 3  | butter   | 0.9  |
| 4  | garlic   | 0.2  |
| 5  | salt     | 0.05 |
| 6  | pepper   | 0.05 |
| 7  | onions   | 0.2  |
| 8  | leeks    | 0.3  |
| 9  | carrots  | 0.2  |
| 10 | peas     | 0.2  |
| 11 | chili    | 0.3  |
| 12 | basil    | 0.3  |
| 13 | milk     | 0.7  |
| 14 | cheese   | 1    |

dish_ingredient pivot table

| dish_id | ingredient_id |
|---------|---------------|
| 1       | 2             |
| 1       | 3             |
| 1       | 6             |
| 1       | 8             |
| 2       | 4             |
| 2       | 8             |
| 2       | 9             |
| 2       | 11            |
| 3       | 1             |
| 3       | 4             |
| 3       | 5             |
| 4       | 6             |
| 4       | 8             |
| 4       | 12            |

You can check out the docs here regarding Eloquent relationships. This sort of stuff is pretty easy to accomplish.

You'd have three models

Chef

public function dishes() {
    return $this->hasMany('App\Dish');
}

Dish

public function ingredients() {
    return $this->belongsToMany('App\Ingredient');
}

Ingredient

public function dishes() {
    return $this->belongsToMany('App\Dish');
}

This should get you going.

1 like
taijuten's avatar

I'd add another table to the above reply, dish_ingredient, as a link table (many to many) between dishes and ingredients, as a dish will have many ingredients.

Sorry for brief reply, on mobile device

mstnorris's avatar

@taijuten I was getting to that but OCD kicked in trying to format the tables, decided on using this little tool to help me out.

amiter00's avatar

Thanks so much for the replies. You guys are awesome!

1 like

Please or to participate in this conversation.