Home >Backend Development >PHP Tutorial >Laravel multi-table association query problem

Laravel multi-table association query problem

WBOY
WBOYOriginal
2016-08-27 09:06:521675browse

Three mysql data tables

  • Product list(product)

  • Package

  • Package information table (package_data).

Main fields of product table:

  • id(self-incremented id)

  • name

  • ...

Main fields of package table:

  • id(self-incremented id)

  • title (package name)

  • ....

Main fields of package information table:

  • id(self-incremented id)

  • product_id (product id)

  • package-id(package id)

  • product_num (number of products)

  • ...

Simple instructions:

There are multiple products in a package. The package table and package information table could have been placed in one table. However, in order to reduce redundant data, the package table was simply divided vertically.

Here comes the question :-)

  • In the above situation, I want to know how many products A are included in package A. So how to define the relationships in the model?

  • In the above situation, I want to know how many products A, product B, etc... are used in package A and package B. So how to operate?

ps: The above questions have been read through the official documents, but my IQ is too low, which leads to unclear understanding, so I am here to ask you for advice.

Looking forward to your enthusiastic guidance ^_^

Reply content:

Three mysql data tables

  • Product list(product)

  • Package

  • Package information table (package_data).

Main fields of product table:

  • id(self-incremented id)

  • name

  • ...

Main fields of package table:

  • id(self-incremented id)

  • title (package name)

  • ....

Main fields of package information table:

  • id(self-incremented id)

  • product_id (product id)

  • package-id(package id)

  • product_num (number of products)

  • ...

Simple instructions:

There are multiple products in a package. The package table and package information table could have been placed in one table. However, in order to reduce redundant data, the package table was simply divided vertically.

Here comes the question :-)

  • In the above situation, I want to know how many products A are included in package A. So how to define the relationships in the model?

  • In the above situation, I want to know how many products A, product B, etc... are used in package A and package B. So how to operate?

ps: The above questions have been read through the official documents, but my IQ is too low, which leads to unclear understanding, so I am here to ask you for advice.

Looking forward to your enthusiastic guidance ^_^

Ahem~, I finally found the answer in the document. Link here . Please search for the "Nested eager loading" section to view it yourself.

Tell me the specific process:

The hasManyData() method is defined in the package model

<code class="php">public function hasManyData()
{
    return $this->hasMany(PackageData::class, 'package_id', 'id');
}</code>

The BelongsToProduct() method is defined in the package_data model

<code class="php">public function BelongsToProduct()
{
    return $this->belongsTo(Product::class, 'product_id', 'id');
}</code>

Used in controller:

<code class="php">$packageList = Package::with('hasManyData', 'package_data.BelongsToProduct')->get();</code>

Done!

Another solution

ps: The method given by a colleague is roughly like this. The following is the pseudo code:

<code class="php">$packageList = Package::with('hasManyData')->get();

foreach( $packageList as $key => $val )
{
    ...
    $productList = PackageData::with('BelongsToProduct')->where(...)->get();
    ...
}</code>

I think the performance is terrible~ and I don’t want to study it anymore! My head is a bit hot, so what I wrote is a bit messy. Please correct me if I am wrong. Thank you to everyone who has watched this issue~

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn