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 动态添加记录
Mar 10 PHP
PHP中将数组转成XML格式的实现代码
Aug 08 PHP
PHP rawurlencode与urlencode函数的深入分析
Jun 08 PHP
Discuz批量替换帖子内容的方法(使用SQL更新数据库)
Jun 23 PHP
ThinkPHP模板比较标签用法详解
Jun 30 PHP
基于PHP代码实现中奖概率算法可用于刮刮卡、大转盘等抽奖算法
Dec 20 PHP
PHP序列化/对象注入漏洞分析
Apr 18 PHP
php array_keys 返回数组的键名
Oct 25 PHP
PHP实现超简单的SSL加密解密、验证及签名的方法示例
Aug 28 PHP
PHP结合Ffmpeg快速搭建流媒体服务的实践记录
Oct 31 PHP
stripos函数知识点实例分享
Feb 11 PHP
php报错502badgateway解决方法
Oct 11 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
收音机史话 - 1960年代前后的DIY
2021/03/02 无线电
PHP得到mssql的存储过程的输出参数功能实现
2012/11/23 PHP
编写php应用程序实现摘要式身份验证的方法详解
2013/06/08 PHP
浅谈php中mysql与mysqli的区别分析
2013/06/10 PHP
PHP中ob_start函数的使用说明
2013/11/11 PHP
thinkphp实现数组分页示例
2014/04/13 PHP
php curl 上传文件代码实例
2015/04/27 PHP
PHP Imagick完美实现图片裁切、生成缩略图、添加水印
2016/02/22 PHP
PHP验证码无法显示的原因及解决办法
2017/08/11 PHP
php+js实现的无刷新下载文件功能示例
2019/08/23 PHP
php 自定义函数实现将数据 以excel 表格形式导出示例
2019/11/13 PHP
js 调用父窗口的具体实现代码
2013/07/15 Javascript
Javascript单元测试框架QUnitjs详细介绍
2014/05/08 Javascript
JavaScript中的Math.LN2属性用法详解
2015/06/12 Javascript
JS获取鼠标选中的文字
2016/08/10 Javascript
Bootstrap显示与隐藏简单实现代码
2017/03/06 Javascript
Java与JavaScript中判断两字符串是否相等的区别
2017/03/13 Javascript
利用百度echarts实现图表功能简单入门示例【附源码下载】
2019/06/10 Javascript
VUE前后端学习tab写法实例
2019/08/06 Javascript
layui select 禁止点击的实现方法
2019/09/05 Javascript
JavaScript 接口原理与用法实例详解
2020/05/12 Javascript
Python中optparse模块使用浅析
2015/01/01 Python
轻松理解Python 中的 descriptor
2017/09/15 Python
python3实现163邮箱SMTP发送邮件
2018/05/22 Python
浅谈Python脚本开头及导包注释自动添加方法
2018/10/27 Python
利用python实现在微信群刷屏的方法
2019/02/21 Python
Python redis操作实例分析【连接、管道、发布和订阅等】
2019/05/16 Python
Python如何实现强制数据类型转换
2019/11/22 Python
python GUI计算器的实现
2020/10/09 Python
MoviePy常用剪辑类及Python视频剪辑自动化
2020/12/18 Python
pycharm 多行批量缩进和反向缩进快捷键介绍
2021/01/15 Python
uniapp+Html5端实现PC端适配
2020/07/15 HTML / CSS
公务员政审单位鉴定材料
2014/05/16 职场文书
幼儿园推普周活动总结
2015/05/07 职场文书
CSS 制作波浪效果的思路
2021/05/18 HTML / CSS
SSM VUE Axios详解
2021/10/05 Vue.js