Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to solve the problem that Laravel associated query returns wrong id

2025-01-27 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly introduces how to solve the problem of Laravel related query returning error id related knowledge, the content is detailed and easy to understand, the operation is simple and fast, has a certain reference value, I believe that you will get something after reading this article on how to solve the problem of Laravel related query returning error id, let's take a look at it.

Using join to associate a query in Laravel Eloquent, if two tables have a field with the same name, such as id, its value will be rewritten by default by a later field with the same name, returning an undesired result. For example, the following associated 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 ()

Both priorities and touch tables have id fields. If the query is constructed in this way, the query result is as shown in the figure:

The Laravel association query returned the wrong id

Here, the value of id is not the id field of the priorities table, but the id field of the touch table. If you print out the executed sql statement:

Select * from `priorities`right join `touch`on `priorities`.`touch _ id` = `touch`.`id`where `priorities`.`type` ='1' order by `priorities`.`total _ score`right join, `touch`.`priorities`at`touch _ id` right join `touch`on `priorities`.`touch _ id` = `touch`.`id`where `priorities`.`type` ='1' order by `priorities`.`total _ score`desc

The query results are as follows:

The result of the query using sql is actually correct, and the id field of another table with the same name is named id1 by default, but the value of id returned by Laravel is not the id field in the figure, but is overridden by the field of another table with the same name.

The solution is to add a field specified by the select method to correctly construct 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 is the end of this article on "how to solve the problem that Laravel related queries return incorrect id". Thank you for reading! I believe that everyone has a certain understanding of the knowledge of "how to solve the problem that Laravel related queries return incorrect id". If you want to learn more, you are welcome to follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report