Click here to Skip to main content
15,891,874 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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.

PHP
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:

PHP
$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.
Posted
Updated 13-Apr-20 9:17am
Comments

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900