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 相关文章推荐
删除无限级目录与文件代码共享
Jul 12 PHP
十天学会php之第八天
Oct 09 PHP
一个高ai的分页函数和一个url函数
Oct 09 PHP
实现了一个PHP5的getter/setter基类的代码
Feb 25 PHP
PHP中几个常用的魔术常量
Feb 23 PHP
细谈php中SQL注入攻击与XSS攻击
Jun 10 PHP
php利用curl抓取新浪微博内容示例
Apr 27 PHP
CodeIgniter安全相关设置汇总
Jul 03 PHP
Yii2中使用join、joinwith多表关联查询
Jun 30 PHP
浅谈php中的循环while、do...while、for、foreach四种循环
Nov 05 PHP
PHP二维数组实现去除重复项的方法【保留各个键值】
Dec 21 PHP
Yii2框架自定义类统一处理url操作示例
May 25 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
基于mysql的论坛(5)
2006/10/09 PHP
初次接触php抽象工厂模式(Elgg)
2010/03/21 PHP
PHP生成腾讯云COS接口需要的请求签名
2018/05/20 PHP
简单实用的js调试logger组件实现代码
2010/11/20 Javascript
jquery实现弹出层完美居中效果
2014/03/03 Javascript
最流行的Node.js精简型和全栈型开发框架介绍
2015/02/26 Javascript
javascript中几个容易混淆的概念总结
2015/04/14 Javascript
无刷新上传文件并返回自定义值
2015/06/11 Javascript
js实现YouKu的漂亮搜索框效果
2015/08/19 Javascript
jQuery新窗口打开外链接
2016/07/21 Javascript
JS 中LocalStorage和SessionStorage的使用
2017/08/17 Javascript
微信小程序实现点击按钮修改文字大小功能【附demo源码下载】
2017/12/06 Javascript
JS求Number类型数组中最大元素方法
2018/04/08 Javascript
jQuery实现点击滚动到指定元素上的方法分析
2020/03/19 jQuery
Python函数学习笔记
2008/10/07 Python
Python 转义字符详细介绍
2017/03/21 Python
Django自定义认证方式用法示例
2017/06/23 Python
python顺序的读取文件夹下名称有序的文件方法
2018/07/11 Python
Python写一个基于MD5的文件监听程序
2019/03/11 Python
Pytoch之torchvision.transforms图像变换实例
2019/12/30 Python
python数据库编程 ODBC方式实现通讯录
2020/03/27 Python
在keras中对单一输入图像进行预测并返回预测结果操作
2020/07/09 Python
python3中数组逆序输出方法
2020/12/01 Python
python实现简单猜单词游戏
2020/12/24 Python
HTML5教程之html 5 本地数据库(Web Sql Database)
2014/04/03 HTML / CSS
HTML5标签使用方法详解
2015/11/27 HTML / CSS
澳大利亚设计的婴儿和女孩的衣服:Oobi
2018/12/16 全球购物
Auguste The Label官网:澳大利亚一家精品女装时尚品牌
2020/06/14 全球购物
如何高效率的查找一个月以内的数据
2012/04/15 面试题
简述Linux文件系统通过i节点把文件的逻辑结构和物理结构转换的工作过程
2016/01/06 面试题
2014年党员承诺书范文
2014/05/20 职场文书
2014年民主评议党员工作总结
2014/12/02 职场文书
2015年小学生国庆节演讲稿
2015/07/30 职场文书
《狮子和鹿》教学反思
2016/02/16 职场文书
Java实现多文件上传功能
2021/06/30 Java/Android
详细了解java监听器和过滤器
2021/07/09 Java/Android