D
D
Daniil Kondratovich2016-06-18 14:45:21
MySQL
Daniil Kondratovich, 2016-06-18 14:45:21

How to create a join table with laravel extra column?

There are two tables: categories, attributes. Each category has several attributes. The category_attribute table is intended to be a link table. Laravel allows you to get attributes that belong to a category and categories that have an attribute. You must set each of the attributes to a specific value for each category. For example, the "description" attribute will be different for each category.
To solve this problem, I want to add a value column to the category_attribute spanning table in addition to the identity columns, which will store the attribute values ​​for the categories. The question is this. How to get all attributes of a category and get the values ​​of those attributes? If the idea with the connecting table is wrong, then tell me in which direction to move.
Thank you.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
D', 2016-06-18
@ktulxu

You can do that too.
Just add value to category_attribute and specify when creating a link:
As a result, the value value can be obtained through:

$attributes = Category::attributes;
foreach ($attributes as $attr) {
  echo $attr->pivot->value, PHP_EOL;
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question