The product package Information Table (package_data) of three mysql data tables ). Main fields in the Product Table: id (auto-increment id) name (name )... main fields in the package table: id (auto-increment id) title (package name ).... package info table main field: id (auto-increment id... three mysql data tables
Product Table)
Package)
Package_data ).
Main fields in the product table:
Id (auto-increment id)
Name)
...
Main fields in the package table:
Id (auto-increment id)
Title (package name)
....
Main fields in the package info table:
Simple description:
A package contains multiple products. The package table and package information table can be stored in one table. to reduce redundant data, a simple vertical table Sharding is performed on the package table.
Question :-)
In the above case, I want to know how many products A is included in package. How can we define the associations in the model?
Under the above circumstances, I would like to know how many products A, B, and etc are used in both Package A and Package B. What should I do?
Ps: I have already reviewed the official documentation for the above questions, but my IQ is too low, which leads to unclear understanding. so I 'd like to ask you.
We look forward to your enthusiastic guidance ^_^
Reply: three mysql data tables
Product Table)
Package)
Package_data ).
Main fields in the product table:
Id (auto-increment id)
Name)
...
Main fields in the package table:
Id (auto-increment id)
Title (package name)
....
Main fields in the package info table:
Simple description:
A package contains multiple products. The package table and package information table can be stored in one table. to reduce redundant data, a simple vertical table Sharding is performed on the package table.
Question :-)
In the above case, I want to know how many products A is included in package. How can we define the associations in the model?
Under the above circumstances, I would like to know how many products A, B, and etc are used in both Package A and Package B. What should I do?
Ps: I have already reviewed the official documentation for the above questions, but my IQ is too low, which leads to unclear understanding. so I 'd like to ask you.
We look forward to your enthusiastic guidance ^_^
Keke ~, Finally, find the answer in the document. Link here. Please search for "nested eager loading" to view it on your own.
Let's talk about the specific process:
Define the hasManyData () method in the package model
public function hasManyData(){ return $this->hasMany(PackageData::class, 'package_id', 'id');}
The BelongsToProduct () method defined in the package_data model
public function BelongsToProduct(){ return $this->belongsTo(Product::class, 'product_id', 'id');}
Controller:
$packageList = Package::with('hasManyData', 'package_data.BelongsToProduct')->get();
Success!
Another solution
Ps: The method provided by a colleague is like this. The following is the pseudocode:
$packageList = Package::with('hasManyData')->get();foreach( $packageList as $key => $val ){ ... $productList = PackageData::with('BelongsToProduct')->where(...)->get(); ...}
I think the performance is too bad ~, I don't want to study any more! My head is a bit hot, so it's a bit messy to write. if not, please make an ax. Thank you for your attention ~