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 相关文章推荐
写一个用户在线显示的程序
Oct 09 PHP
动态新闻发布的实现及其技巧
Oct 09 PHP
PHP加速 eAccelerator配置和使用指南
Jun 05 PHP
PHP初学者最感迷茫的问题小结
Mar 27 PHP
PHP函数篇之掌握ord()与chr()函数应用
Dec 05 PHP
php实现单链表的实例代码
Mar 22 PHP
php中常见的sql攻击正则表达式汇总
Nov 06 PHP
php使用crypt()函数进行加密
Jun 08 PHP
Laravel学习基础之migrate的使用教程
Oct 11 PHP
Laravel框架运行出错提示RuntimeException No application encryption key has been specified.解决方法
Apr 02 PHP
Thinkphp5.0 框架实现控制器向视图view赋值及视图view取值操作示例
Oct 12 PHP
Yii实现微信公众号场景二维码的方法实例
Aug 30 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
Windows中使用计划任务自动执行PHP程序实例
2014/05/09 PHP
php实现mysql数据库分表分段备份
2015/06/18 PHP
PHP实现redis限制单ip、单用户的访问次数功能示例
2018/06/16 PHP
php引用和拷贝的区别知识点总结
2019/09/23 PHP
分析 JavaScript 中令人困惑的变量赋值
2007/08/13 Javascript
jquery load()在firefox(火狐)下显示不正常的解决方法
2011/04/05 Javascript
js 图片随机不定向浮动的实现代码
2013/07/02 Javascript
利用js(jquery)操作Cookie的方法说明
2013/12/19 Javascript
JQuery对表单元素的基本操作使用总结
2014/07/18 Javascript
javascript实现在某个元素上阻止鼠标右键事件的方法和实例
2014/08/12 Javascript
Angularjs注入拦截器实现Loading效果
2015/12/28 Javascript
node模块机制与异步处理详解
2016/03/13 Javascript
jQuery使用each方法与for语句遍历数组示例
2016/06/16 Javascript
基于easyui checkbox 的一些操作处理方法
2017/07/10 Javascript
解决canvas画布使用fillRect()时高度出现双倍效果的问题
2017/08/03 Javascript
Node Puppeteer图像识别实现百度指数爬虫的示例
2018/02/22 Javascript
学习node.js 断言的使用详解
2019/03/18 Javascript
vue 解决mintui弹窗弹起来,底部页面滚动bug问题
2020/11/12 Javascript
[01:41]DOTA2 2015国际邀请赛中国区预选赛第三日战报
2015/05/28 DOTA
Keras搭建自编码器操作
2020/07/03 Python
python利用 keyboard 库记录键盘事件
2020/10/16 Python
基于Python爬取京东双十一商品价格曲线
2020/10/23 Python
HTML5 progress和meter控件_动力节点Java学院整理
2017/07/06 HTML / CSS
Html5 Canvas 实现一个“刮刮乐”游戏
2019/09/05 HTML / CSS
Canvas绘制浮动球效果的示例
2017/12/29 HTML / CSS
HTML5 WebSocket实现点对点聊天的示例代码
2018/01/31 HTML / CSS
Java编程面试题
2016/04/04 面试题
公司活动邀请函
2014/01/24 职场文书
文明和谐家庭事迹材料
2014/05/18 职场文书
cf战队收人口号
2014/06/21 职场文书
医学专业大学生求职信
2014/07/12 职场文书
基层党建工作汇报材料
2014/08/15 职场文书
保护校园环境倡议书
2015/04/28 职场文书
实践论读书笔记
2015/06/29 职场文书
Python 实现绘制子图及子图刻度的变换等问题
2021/05/31 Python
css3中2D转换之有趣的transform形变效果
2022/02/24 HTML / CSS