Laravel -- has Method -- View associations, laravel -- has --

Source: Internet
Author: User

Laravel -- has Method -- View associations, laravel -- has --

The has () method can be used to query whether there is a correlation. Generally, the other has method is used to determine whether there is a value in it.

$packageOrders = Company::has('packages')->get();
dd($packageOrders);

The has method in this sentence has different meanings. What is his detailed logic, how to associate it, and how to query it. First, let's take a look at what dd ($ packageOrders) outputs.

We can see that the output is also the company element. The total data obtained is 18.

$packageOrders = Company::all();  dd($packageOrders);

We can see that there are more than 100 printed data, so the has method uses something to filter, resulting in only 18 results.

$ PackageOrders = Company: has ('packages ')-> get ();
The detailed meaning of this sentence is to first go to company to find the table associated with packages to see which data they match each other's IDs, first, go to the company model to find the corresponding
Packages Method

Then, the packages method is associated with the app \ CompanyPackage model. The associated ID is company_id, and the third parameter indicates matching the company_id in the companyPackage with its own ID value.
Then, the database process is to first look for the id value in the company table. In the company_packages table, check the value of company_id. If there is any match, retrieve all the matched data, the matching data is extracted from the company table and will not be retrieved.
The data in the company_packages table, that is, the data displayed at the beginning of the article.

What if the second parameter of the packages method in the company model is changed to package_id?

Then, $ packageOrders = Company: has ('packages ')-> get (); the IDs in the company table and the package_id in the company_packages table are displayed in the database,
Filtering is like this, so the all () method has more than 100 pieces of data, while has only has 18 pieces of data that match each other.




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.