I'm building an ecommerce with a big menu where there are like 10 main categories, each of which contains many lower-level subcategories inside. For example, a main category "PC components" contains two sub-category of CPUs and Mainboards, then the CPU sub-category contains Intel and AMD, then Intel contains i3, i5, i7. I spent some hours researching and found that the problem might be solved by relational database with parent-child relations and nested foreach loops, somehow like below
//Controller
... compact('maincategories')->toArray (//each compacted maincategory array should contain all children and grandchildren, etc.);
//View
@foreach($maincategories as $maincategory)
<h1>{{$maincategory['name']}}</h1>
@foreach($maincategory['subcategories'] as $subcategory
<h2>{{$subcategory['name']}}</h2>
@foreach($subcategory['subcategory_level_2s'] as $subcategory_level_2)
<h3>{{$subcategory_level_2['name']}}</h3>
@endforeach
@endforeach
@endforeach
However, I don't know how to set up relationship between them. I already know hasMany and belongsTo relations, but how to make them relate to each other is confusing. Any suggestion is appreciated. Thanks.
Hours spent. Can't set up the relations with sql table.