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 和 MySQL 基础教程(三)
Oct 09 PHP
php UBB 解析实现代码
Nov 27 PHP
PHP 基于Yii框架中使用smarty模板的方法详解
Jun 13 PHP
php实现aes加密类分享
Feb 16 PHP
PHP程序漏洞产生的原因分析与防范方法说明
Mar 06 PHP
Smarty最简单实现列表奇偶变色的方法
Jul 01 PHP
WordPress中访客登陆实现邮件提醒的PHP脚本实例分享
Dec 14 PHP
今天你说520了吗?不仅有php表白书还有java表白神器
May 20 PHP
php对接java现实加签验签的实例
Nov 25 PHP
PHP基于PDO扩展操作mysql数据库示例
Dec 24 PHP
PHP迭代器和生成器用法实例分析
Sep 28 PHP
php实现文件上传基本验证
Mar 04 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.MVC的模板标签系统(一)
2006/09/05 PHP
PHP4实际应用经验篇(9)
2006/10/09 PHP
基于wordpress主题制作的具体实现步骤
2013/05/10 PHP
PHP输出当前进程所有变量/常量/模块/函数/类的示例
2013/11/07 PHP
php实现根据词频生成tag云的方法
2015/04/17 PHP
PHP实现的登录页面信息提示功能示例
2017/07/24 PHP
兼容IE和Firefox的javascript获取iframe文档内容的函数
2011/08/15 Javascript
NodeJS学习笔记之网络编程
2014/08/03 NodeJs
jQuery获取字符串中出现最多的数
2016/02/22 Javascript
Javascript实现通过选择周数显示开始日和结束日的实现代码
2016/05/30 Javascript
微信小程序 canvas API详解及实例代码
2016/10/08 Javascript
javascript修改浏览器title方法 JS动态修改浏览器标题
2017/11/30 Javascript
深入解析koa之异步回调处理
2019/06/17 Javascript
ES6 新增的创建数组的方法(小结)
2019/08/01 Javascript
Pyhton中防止SQL注入的方法
2015/02/05 Python
python用来获得图片exif信息的库实例分析
2015/03/16 Python
举例讲解Python编程中对线程锁的使用
2016/07/12 Python
Python首次安装后运行报错(0xc000007b)的解决方法
2016/10/18 Python
Python加密方法小结【md5,base64,sha1】
2017/07/13 Python
python实现电脑自动关机
2018/06/20 Python
Python产生Gnuplot绘图数据的方法
2018/11/09 Python
利用Pandas和Numpy按时间戳将数据以Groupby方式分组
2019/07/22 Python
Python如何读取文件中图片格式
2020/01/13 Python
Clearly澳大利亚:购买眼镜、太阳镜和隐形眼镜
2018/04/26 全球购物
马来西亚最大的在线隐形眼镜商店:MrLens
2019/03/27 全球购物
优秀团员个人事迹材料
2014/01/29 职场文书
运动会入场词50字
2014/02/20 职场文书
好听的队名和口号
2014/06/09 职场文书
商业计算机应用专业自荐书
2014/06/09 职场文书
优秀会计求职信
2014/07/04 职场文书
交警作风整顿剖析材料
2014/10/11 职场文书
老人院义工活动感想
2015/08/07 职场文书
mysql 数据插入优化方法之concurrent_insert
2021/07/01 MySQL
RPM包方式安装Oracle21c的方法详解
2021/08/23 Oracle
Elasticsearch 数据类型及管理
2022/04/19 Python
springboot创建的web项目整合Quartz框架的项目实践
2022/06/21 Java/Android