Laravel association query

Source: Internet
Author: User
1. two tables: MERs and contacts. The customers customer table has the idnameaddress field contacts Contact table and the idnametelqqcustomer_id field. One customer has multiple contacts. 2. fuzzy search for the daily drop contact tel to list the MERs table. If you use join... 1. two tables: MERs and contacts
The customers customer table has the id name address field.
The contacts Contact table has the id name tel qq mermer_id field.
One customer has multiple contacts
2. fuzzy search for the daily drop contact tel to list the MERs table. If you use the join method, duplicate results will be displayed, the laravel associated query orm cannot store the correct contacts in the contact array in the customers result like the tp association model? We also need to consider paging ..

Update
Sorry, the reply is too late. Thank you for your updates. I spent some time using the following methods at night. First, I tried the @ daniel_wu Wei method and the pre-loading method in laravel manual.
I need to search for customers whose contact phone number contains number 8. In Customer 4, contact 3, contact 5, and contact 2 meet the conditions. The goal is to query the two customers.

The Code is as follows:

$list = Customer::with(['Contacts' => function ($query) use ($request) {            if ($request->has('tel')) {                $query->where('tel', 'like', '%' . $request->input('tel') . '%');            }        }])->get();

Then I printed the $ list
The Print Code is as follows:

foreach ($list as $data) {            echo $data->name;            foreach ($data->contacts as $item) {                echo '  ';                echo $item->name;            }            echo '
'; }

The output is as follows:

The SQL code is as follows:

select * from `customers` where `customers`.`deleted_at` is nullselect * from `contacts` where `contacts`.`deleted_at` is null and `contacts`.`customer_id` in ('37', '38', '39', '40', '41') and `tel` like '%8%'

We can see that customers 1, 2, and 3 that do not need to be queried only filter out qualified contacts.

Then let's look at the @ dawniii method.

       $list = Customer::whereHas('Contacts', function ($query) use ($request) {            if ($request->has('tel')) {                $query->where('tel', 'like', '%' . $request->input('tel') . '%');            }        })->get();

Result

SQL code

select * from `customers` where `customers`.`deleted_at` is null and (select count(*) from `contacts` where `contacts`.`customer_id` = `customers`.`id` and `tel` like '%8%' and `contacts`.`deleted_at` is null) >= 1select * from `contacts` where `contacts`.`deleted_at` is null and `contacts`.`customer_id` = '40' and `contacts`.`customer_id` is not nullselect * from `contacts` where `contacts`.`deleted_at` is null and `contacts`.`customer_id` = '41' and `contacts`.`customer_id` is not null

If there is a problem with my description, let's say that there is an offset and I'm sorry,
This question gives me a lot of understanding about laravel's orm. If you have any mistakes, please correct me and give me some suggestions. laravel has very few practical orm content. Thank you for your enthusiastic answers.

If you want a non-repeated list of MERs

$list = Customers::whereHas('Contacts', function($q){    $q->where('tel', 'like', '%foo%');})->get();

Check the manual more.

$list =Contacts::with(['Customers', function ($q) {    // sub query  }])->get()

If you remember correctly, it should be like this.

I did not expect a better solution for the moment.
First, identify the customer_id and distinct () deduplication of the qualified contacts table, and then the customer_id obtained from the wmers table whereIn ().

ORM does not need to replace SQL, but some query statements in zookeeper may still need to directly execute SQL statements, but ORM gives us more weapons, for example, the addition and modification of the merge Division for a single worker is very suitable for using the ORM method.

ORM is not suitable for complex ing relationships. Use the db class.

Related Articles:

Question about the association query of multiple Laravel conditions?

Laravel associated query only obtains part of the data of the management object

Laravel

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.