I'm new to larevel and experimenting with some projects. I current project I am stuck on relations and eloquent model. If someone can help me to understand to way to do it?
I have 3 models with their respective tables:
car:
- id
- name
feature:
- id
- name
- group_id
featureGroup:
- id
- name
Next I also have a 4th table to create manyToMany relations between Features and Cars.
_car__feature:
- car_id
- feature_id
Now everything is working but I'm not sure how to get the Features grouped by Feature group in my Cars Controller (to show grouped features) on single car layout.
For example:
Car::where('slug', $slug)->first()->load('features.group');
This returns a single car and as a child collection it's features. But here the feature group is a child of the feature.
So it's car->features->featureGroup.
But I want to return car->featureGroup->features, so I can loop over the featureGroups in my blade layout and return the features grouped.
How would you go about this in Eloquent?
Or am I thinking the wrong way, should I somehow set a relationship to featureGroups() inside the Car model, instead of features()?
Thanks