Home>Article>Backend Development> Detailed explanation of the solution to the error id returned by Laravel related query in PHP
This article mainly introduces the solution to the error id returned by Laravel's related query. It is very good and has reference value. Friends who need it can refer to it.
Use join related query in Laravel Eloquent. If two If a table has a field with the same name, such as id, then its value will be overwritten by a later field with the same name by default, and the result returned is not expected. For example, the following related 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 the priorities and touch tables have id fields. If the query is constructed like this, the returned query results are as follows:
Laravel related query returns the wrong id
The value of id here is not the id field of the priorities table, but the id field of the touch table. If the executed sql statement is printed out:
select * from `priorities` right join `touch` on `priorities`.`touch_id` = `touch`.`id` where `priorities`.`type` = '1' order 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' order by `priorities`.`total_score` desc, `touch`.`created_at` desc
The query result is as shown in the figure:
The result of using sql query is actually correct. In addition The id field of a table with the same name is named id1 by default, but the id value returned by Laravel is not the id field in the figure, but has been overwritten by the field of another table with the same name.
The solution is to add a select method to specify the field and correctly construct the query statement code:
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, so you should pay attention to it in the future. It is best to specify the fields returned when joining two tables in Laravel.
Is this a bug in Laravel? If the value of a field is overwritten by a field value with the same name, should an error be reported instead of continuing execution by default?
Someone on github also raised the same problem, and the author also provided a solution, but there is no other better solution.
Laravel version: 5.3
Link: https://github.com/laravel/framework/issues/4962
The above is the entire content of this article, I hope it will be helpful to everyone Learning helps.
Related recommendations:
DVWA's php mysql manual injection
PHPproject integration WeChat terminal scan code payment API (domestic payment)
The above is the detailed content of Detailed explanation of the solution to the error id returned by Laravel related query in PHP. For more information, please follow other related articles on the PHP Chinese website!