I'm trying to extract product categories from a csv file. The categories are hierarchical, but in string form like this: Parent,Child,Grandchild
.
Each row in the csv file is a product, and the categories are repeated for every product.
I'm working with PHP 7.2, MySQL 5.7 and Laravel 5.8.
I don't even know where to start. I'm usually good at hacking something together that works, and then continuing working on it until it's looking good. But this time I'm kind of stumped.
So, to summarize:
I would like to loop through strings like these:
Parent,Child
Parent,Child,Grandchild
Parent,Other Child,Other Grandchild
Other Parent,Different Child,Different Grandchild
And get this organized into my MySQL database:
| id | parent_id | title |
| 1 | null | Parent |
| 2 | 1 | Child |
| 3 | 2 | Grandchild |
| 4 | 1 | Other Child |
| 5 | 4 | Other Grandchild |
| 6 | null | Other Parent |
| 7 | 6 | Different Child |
| 8 | 7 | Different Grandchild |
Duplicates (by title) should just be merged, but not if they have a different parent.
Maybe there's some better way to organize it in the database to make this easier? I'm open to suggestions if you have any. Thanks in advance.