PHP中Laravel 关联查询返回错误id的解决方法


Posted in PHP onApril 01, 2017

在 Laravel Eloquent 中使用 join 关联查询,如果两张表有名称相同的字段,如 id,那么它的值会默认被后来的同名字段重写,返回不是期望的结果。例如以下关联查询:

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 和 touch 这两张表都有 id 字段,如果这样构造查询的话,返回的查询结果如图:

PHP中Laravel 关联查询返回错误id的解决方法

Laravel 关联查询返回错误的 id

这里 id 的值不是 priorities 表的 id 字段,而是 touch 表的 id 字段,如果打印出执行的 sql 语句:

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

查询结果如图:

PHP中Laravel 关联查询返回错误id的解决方法

使用 sql 查询的结果实际上是对的,另外一张表重名的 id 字段被默认命名为 id1,但是 Laravel 返回的 id 的值却不是图中的 id 字段,而是被重名的另外一张表的字段重写了。

解决办法是加一个 select 方法指定字段,正确的构造查询语句的代码:

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();

这样就解决了问题,那么以后就要注意了,Laravel 两张表 join 的时候返回的字段最好要指定。

这算不算是 Laravel 的一个 bug 呢?如果一个字段的值被同名的字段值重写了,这种情况要不要报一个错误出来,而不能默认继续执行下去。

github 上有人也提出了同样的问题,作者也提供了解决办法,但并没其他更好的方案。

Laravel 版本:5.3

链接:https://github.com/laravel/framework/issues/4962

以上所述是小编给大家介绍的Laravel 关联查询返回错误的 id的解决方法,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对三水点靠木网站的支持!

PHP 相关文章推荐
PHP生成静态页
Nov 25 PHP
php中var_export与var_dump的区别分析
Aug 21 PHP
zen cart新进商品的随机排序修改方法
Sep 10 PHP
PHP substr 截取字符串出现乱码问题解决方法[utf8与gb2312]
Dec 16 PHP
PHP编码规范的深入探讨
Jun 06 PHP
PHP连接MSSQL2008/2005数据库(SQLSRV)配置实例
Oct 22 PHP
php 魔术方法详解
Nov 11 PHP
CodeIgniter分页类pagination使用方法示例
Mar 28 PHP
PHP多进程编程总结(推荐)
Jul 18 PHP
php实现将base64格式图片保存在指定目录的方法
Oct 13 PHP
PHP实现动态删除XML数据的方法示例
Mar 30 PHP
解决PHPstudy Apache无法启动的问题【亲测有效】
Oct 30 PHP
php获取ip及网址的简单方法(必看)
Apr 01 #PHP
Thinkphp事务操作实例(推荐)
Apr 01 #PHP
完美解决thinkphp唯一索引重复时出错的问题
Mar 31 #PHP
ThinkPHP Where 条件中常用表达式示例(详解)
Mar 31 #PHP
浅谈PHP的exec()函数无返回值排查方法(必看)
Mar 31 #PHP
关于PHP通用返回值设置方法
Mar 31 #PHP
PHP针对中英文混合字符串长度判断及截取方法示例
Mar 31 #PHP
You might like
php知道与问问的采集插件代码
2010/10/12 PHP
php验证码生成代码
2015/11/11 PHP
php批量删除操作(数据访问)
2017/05/23 PHP
Maps Javascript
2007/01/22 Javascript
javascript操作table(insertRow,deleteRow,insertCell,deleteCell方法详解)
2013/12/16 Javascript
JavaScript检查弹出窗口是否被阻拦的方法技巧
2015/03/13 Javascript
JS 组件系列之 bootstrap treegrid 组件封装过程
2017/04/28 Javascript
微信小程序 WXML节点信息查询详解
2019/07/29 Javascript
微信小程序 云开发模糊查询实现解析
2019/09/02 Javascript
JavaScript修改注册表实例代码
2020/01/05 Javascript
JS监听组合按键思路及实现过程
2020/04/17 Javascript
React中Ref 的使用方法详解
2020/04/28 Javascript
python 实现网上商城,转账,存取款等功能的信用卡系统
2016/07/15 Python
轻松实现TensorFlow微信跳一跳的AI
2018/01/05 Python
python中ASCII码和字符的转换方法
2018/07/09 Python
pandas 数据结构之Series的使用方法
2019/06/21 Python
python高斯分布概率密度函数的使用详解
2019/07/10 Python
Python Scrapy框架第一个入门程序示例
2020/02/05 Python
简单了解django文件下载方式
2020/02/10 Python
python 控制台单行刷新,多行刷新实例
2020/02/19 Python
浅谈keras 的抽象后端(from keras import backend as K)
2020/06/16 Python
html5组织文档结构_动力节点Java学院整理
2017/07/11 HTML / CSS
美国卡车、吉普车和SUV零件网站:4 Wheel Parts
2016/11/24 全球购物
Oroton中国官网:澳洲知名奢侈配饰品牌
2017/03/26 全球购物
后勤人员自我评价怎么写
2013/09/19 职场文书
专业幼师实习生自我鉴定范文
2013/12/08 职场文书
好邻里事迹材料
2014/01/16 职场文书
写求职信有什么意义
2014/02/17 职场文书
优秀团队获奖感言
2014/02/19 职场文书
学生党员的自我评价范文
2014/03/01 职场文书
保护环境的建议书
2014/03/12 职场文书
装修活动策划方案
2014/08/27 职场文书
作风转变年心得体会
2014/10/22 职场文书
商场圣诞节活动总结
2015/05/06 职场文书
Linux、ubuntu系统下查看显卡型号、显卡信息详解
2022/04/07 Servers
MySql数据库触发器使用教程
2022/06/01 MySQL