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 相关文章推荐
使用无限生命期Session的方法
Oct 09 PHP
mayfish 数据入库验证代码
Apr 30 PHP
php strrpos()与strripos()函数
Aug 31 PHP
PHP实现查询两个数组中不同元素的方法
Feb 23 PHP
php编程中echo用逗号和用点号连接的区别
Mar 26 PHP
PHP 极验验证码实例讲解
Sep 29 PHP
php中namespace及use用法分析
Dec 06 PHP
php实现评论回复删除功能
May 23 PHP
实现PHP中session存储及删除变量
Oct 15 PHP
Smarty缓存机制实例详解【三种缓存方式】
Jul 20 PHP
解决laravel上传图片之后,目录有图片,但是访问不到(404)的问题
Oct 14 PHP
php中加密解密DES类的简单使用方法示例
Mar 26 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使用APC实现实时上传进度条功能
2015/10/26 PHP
PHP的PDO预处理语句与存储过程
2019/01/27 PHP
js 表单验证方法(实用)
2009/04/28 Javascript
node.js中的http.response.setHeader方法使用说明
2014/12/14 Javascript
JavaScript中的null和undefined区别介绍
2015/01/01 Javascript
jQuery实现鼠标经过事件的延时处理效果
2020/08/20 Javascript
jQuery使用$.ajax进行异步刷新的方法(附demo下载)
2015/12/04 Javascript
利用css+原生js制作简单的钟表
2020/04/07 Javascript
jQuery+json实现动态创建复杂表格table的方法
2016/10/25 Javascript
Bootstrap学习笔记之环境配置(1)
2016/12/07 Javascript
Javascript创建类和对象详解
2017/05/31 Javascript
vue 使用html2canvas将DOM转化为图片的方法
2018/09/11 Javascript
微信小程序实现笑脸评分功能
2018/11/03 Javascript
CKEditor 4.4.1 添加代码高亮显示插件功能教程【使用官方推荐Code Snippet插件】
2019/06/14 Javascript
小程序实现左滑删除效果
2019/07/25 Javascript
ES6的异步操作之promise用法和async函数的具体使用
2019/12/06 Javascript
VUE项目axios请求头更改Content-Type操作
2020/07/24 Javascript
Bootstrap告警框(alert)实现弹出效果和短暂显示后上浮消失的示例代码
2020/08/27 Javascript
Nuxt.js 静态资源和打包的操作
2020/11/06 Javascript
[06:16]《DAC最前线》之地区预选赛全面回顾
2015/01/19 DOTA
[01:14:41]DOTA2-DPC中国联赛定级赛 iG vs Magma BO3第一场 1月8日
2021/03/11 DOTA
Linux环境下MySQL-python安装过程分享
2015/02/02 Python
python使用Apriori算法进行关联性解析
2017/12/21 Python
DataFrame中的object转换成float的方法
2018/04/10 Python
Python实现按逗号分隔列表的方法
2018/10/23 Python
Pyqt QImage 与 np array 转换方法
2019/06/27 Python
Python 使用 Pillow 模块给图片添加文字水印的方法
2019/08/30 Python
python超时重新请求解决方案
2019/10/21 Python
CSS+jQuery实现的在线答题功能
2015/04/25 HTML / CSS
浅析CSS3 用text-overflow解决文字排版问题
2020/10/28 HTML / CSS
琳达·法罗眼镜英国官网:Linda Farrow英国
2021/01/19 全球购物
C/C++程序员常见面试题一
2012/12/08 面试题
揭牌仪式策划方案
2014/05/28 职场文书
2015年学校总务工作总结
2015/07/20 职场文书
Redis集群的关闭与重启操作
2021/07/07 Redis
深入讲解Vue中父子组件通信与事件触发
2022/03/22 Vue.js