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 相关文章推荐
如何将数据从文本导入到mysql
Oct 09 PHP
基于xcache的配置与使用详解
Jun 18 PHP
php strrpos()与strripos()函数
Aug 31 PHP
php中{}大括号是什么意思
Dec 01 PHP
PHP计算指定日期所在周的开始和结束日期的方法
Mar 24 PHP
基于php的微信公众平台开发入门实例
Apr 15 PHP
php通过排列组合实现1到9数字相加都等于20的方法
Aug 03 PHP
mac下多个php版本快速切换的方法
Oct 09 PHP
PHP实现递归目录的5种方法
Oct 27 PHP
php使用mysqli和pdo扩展,测试对比连接mysql数据库的效率完整示例
May 09 PHP
基于PHP实现微信小程序客服消息功能
Aug 12 PHP
关于PHP5.6+版本“No input file specified”问题的解决
Dec 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
php str_pad() 将字符串填充成指定长度的字符串
2010/02/23 PHP
php中HTTP_REFERER函数用法实例
2014/11/21 PHP
js无刷新操作table的行和列
2014/03/27 Javascript
Javascript基础教程之if条件语句
2015/01/18 Javascript
jquery结合CSS使用validate实现漂亮的验证
2015/01/29 Javascript
使用Javascript监控前端相关数据的代码
2016/10/27 Javascript
javascript 删除数组元素和清空数组的简单方法
2017/02/24 Javascript
Javascript中八种遍历方法的执行速度深度对比
2017/04/25 Javascript
详解axios在vue中的简单配置与使用
2017/05/10 Javascript
vue-cli axios请求方式及跨域处理问题
2018/03/28 Javascript
vue实现组件之间传值功能示例
2018/07/13 Javascript
layui关闭弹窗后刷新主页面和当前更改项的例子
2019/09/06 Javascript
JavaScript this关键字指向常用情况解析
2020/09/02 Javascript
Python安装第三方库及常见问题处理方法汇总
2016/09/13 Python
python中利用Future对象异步返回结果示例代码
2017/09/07 Python
Python实现合并同一个文件夹下所有PDF文件的方法示例
2018/04/28 Python
python 匹配url中是否存在IP地址的方法
2018/06/04 Python
使用python3实现操作串口详解
2019/01/01 Python
Python人工智能之路 jieba gensim 最好别分家之最简单的相似度实现
2019/08/13 Python
python数据爬下来保存的位置
2020/02/17 Python
python如何快速生成时间戳
2020/07/21 Python
Html5 webview元素定位工具的实现
2020/08/07 HTML / CSS
美国排名第一的在线葡萄酒商店:Wine.com
2016/09/07 全球购物
台湾时尚彩瞳专门店:imeime
2019/08/16 全球购物
电脑专业个人求职信范文
2014/02/04 职场文书
安全教育月活动总结
2014/05/05 职场文书
责任心演讲稿
2014/05/14 职场文书
解除劳动合同协议书范本2014
2014/09/25 职场文书
党的群众路线教育实践活动个人整改措施材料
2014/11/04 职场文书
党小组推荐意见
2015/06/02 职场文书
2016年幼儿园教师师德承诺书
2016/03/25 职场文书
小学四年级作文之最感动的一件事
2019/11/01 职场文书
Spring mvc是如何实现与数据库的前后端的连接操作的?
2021/06/30 Java/Android
Mysql关于数据库是否应该使用外键约束详解说明
2021/10/24 MySQL
【海涛dota解说】海涛小满开黑4v5被破两路翻盘潮汐第一视角解说
2022/04/01 DOTA
Golang 1.18 多模块Multi-Module工作区模式的新特性
2022/04/11 Golang