I have a MySQL DB with relationships between tables.
Table cars have foreign keys to the following tables:
- brands
- model
- generations
- engine_modifications
Models have a foreign key to brands, generations have a foreign key to models, engine_modifications to generations.
When I try to render all generations for certain model the number of queries to the DB is enormous.
public function show($id)
{
$model = Model::findOrFail($id);
return view('models.show')->withModel($model);
}
What I have tried:
I thought this is related to n+1 problem and I tried to point out the relations:
$model = Model::with(
'generations',
'brand',
'generations.engineModifications',
'generations.model',
'generations.cars'
)->findOrFail($id);
Unfortunately, this does not fix my problem either. The number of queries is the same for each model.