Workaround for Laravel associated query return error ID in PHP

Source: Internet
Author: User
This article mainly introduces the Laravel associated query return error ID resolution, very good, with reference value, need to refer to the friend

Using the Join Association query in Laravel eloquent, if the two tables have fields with the same name, such as ID, then its value is overridden by default by the subsequent field with the same names, and returns a result that is not expected. For example, the following association query:

Php

$priority = Priority::rightjoin (' Touch ', ' priorities.touch_id ', ' = ', ' touch.id ')->where (' Priorities.type ', 1)- >orderby (' Priorities.total_score ', ' desc ')->orderby (' Touch.created_at ', ' desc ')->get ();

$priority = Priority::rightjoin (' Touch ', ' priorities.touch_id ', ' = ', ' touch.id ')->where (' Priorities.type ', 1)- >orderby (' Priorities.total_score ', ' desc ')->orderby (' Touch.created_at ', ' desc ')->get ();

Priorities and touch both tables have ID fields, and if you construct queries like this, the results of the query returned

Laravel Association query returns the ID of the error

The value of this ID is not the ID field of the priorities table, but the ID field of the touch table, if the SQL statement executed is printed:

SELECT * from ' priorities ' right join ' touch ' on ' priorities '. ' touch_id ' = ' touch '. ' id ' where ' priorities '. ' Type ' = ' 1 ' or Der by ' priorities '. ' Total_score ' desc, ' touch '. ' Created_at ' desc

SELECT * from ' priorities ' right join ' touch ' on ' priorities '. ' touch_id ' = ' touch '. ' id ' where ' priorities '. ' Type ' = ' 1 ' or Der by ' priorities '. ' Total_score ' desc, ' touch '. ' Created_at ' desc

Query results

The result of using the SQL query is actually true, and the ID field with the name of the other table is named ID1 by default, but the value of the ID returned by Laravel is not the ID field in the diagram, but it is rewritten by the field of the other table with the duplicate name.

The workaround is to add a Select method that specifies the field and correctly constructs the code for the query statement:

Php

$priority = Priority::select ([' priorities.* ', ' touch.name ', ' Touch.add_user '])->rightjoin (' Touch ', ' priorities.touch_id ', ' = ', ' touch.id ')->where (' Priorities.type ', 1)->orderby (' Priorities.total_score ', ' Desc ')->orderby (' Touch.created_at ', ' desc ')->get ();

$priority = Priority::select ([' priorities.* ', ' touch.name ', ' Touch.add_user '])->rightjoin (' Touch ', ' priorities.touch_id ', ' = ', ' touch.id ')->where (' Priorities.type ', 1)->orderby (' Priorities.total_score ', ' Desc ')->orderby (' Touch.created_at ', ' desc ')->get ();

This solves the problem, then you should pay attention later, Laravel two table join when the return field is best to specify.

Is this a bug of Laravel? If the value of a field is rewritten with a field value of the same name, do not report an error, and you cannot continue execution by default.

The same problem was raised on GitHub, and the authors offered solutions, but there was no better solution.

Laravel Version: 5.3

Links: https://github.com/laravel/framework/issues/4962

The above is the whole content of this article, I hope that everyone's study has helped.


Related Article

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.