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

Laravel multi-table association query problem

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
Release: 2016-08-27 09:06:52
Original
1686 people have browsed it

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>
Copy after login

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>
Copy after login

Used in controller:

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

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>
Copy after login

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~

Related labels:
source:php.cn
Statement of this Website
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
Latest Issues
php data acquisition?
From 1970-01-01 08:00:00
0
0
0
PHP extension intl
From 1970-01-01 08:00:00
0
0
0
How to learn php well
From 1970-01-01 08:00:00
0
0
0
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template