Laravel multi-table Association query

Source: Internet
Author: User
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:
  • Id (auto-increment id)

  • Product_id (product id)

  • Package-id (package id)

  • Product_num (product quantity)

  • ...

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:
  • Id (auto-increment id)

  • Product_id (product id)

  • Package-id (package id)

  • Product_num (product quantity)

  • ...

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 ~

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.