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学习之PHP运算符
Oct 09 PHP
php array_flip() 删除数组重复元素
Jan 14 PHP
php根据日期判断星座的函数分享
Feb 13 PHP
php判断ip黑名单程序代码实例
Feb 24 PHP
PHP中使用循环实现的金字塔图形
Nov 08 PHP
php post大量数据时发现数据丢失问题解决方法
Jun 20 PHP
CodeIgniter连贯操作的底层原理分析
May 17 PHP
PHP中header用法小结
May 23 PHP
CentOS 7.2 下编译安装PHP7.0.10+MySQL5.7.14+Nginx1.10.1的方法详解(mini版本)
Sep 01 PHP
深入理解Yii2.0乐观锁与悲观锁的原理与使用
Jul 26 PHP
laravel5.6 框架操作数据 Eloquent ORM用法示例
Jan 26 PHP
phpQuery采集网页实现代码实例
Apr 02 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封装分页函数实现文本分页和数字分页
2014/10/23 PHP
Windows下编译PHP5.4和xdebug全记录
2015/04/03 PHP
Zend Framework教程之配置文件application.ini解析
2016/03/10 PHP
PHP高并发和大流量解决方案整理
2019/12/24 PHP
关于Javascript 的 prototype问题。
2007/01/03 Javascript
动态载入/删除/更新外部 JavaScript/Css 文件的代码
2010/07/03 Javascript
Node.js:Windows7下搭建的Node.js服务(来玩玩服务器端的javascript吧,这可不是前端js插件)
2011/06/27 Javascript
使用phantomjs进行网页抓取的实现代码
2014/09/29 Javascript
jQuery使用之标记元素属性用法实例
2015/01/19 Javascript
基于Bootstrap3表格插件和分页插件实例详解
2016/05/17 Javascript
Bootstrap的Carousel配合dropload.js实现移动端滑动切换图片
2017/03/10 Javascript
jquery.uploadifive插件怎么解决上传限制图片或文件大小问题
2017/05/08 jQuery
javascript简单链式调用案例分析
2017/05/10 Javascript
详解nodejs异步I/O和事件循环
2017/06/07 NodeJs
vue将时间戳转换成自定义时间格式的方法
2018/03/02 Javascript
vue.js添加一些触摸事件以及安装fastclick的实例
2018/08/28 Javascript
详解JavaScript的内存空间、赋值和深浅拷贝
2019/04/17 Javascript
详解简单易懂的 ES6 Iterators 指南和示例
2019/09/24 Javascript
Vue中jsx不完全应用指南小结
2019/11/01 Javascript
vue接口请求加密实例
2020/08/11 Javascript
浅谈Python中的作用域规则和闭包
2018/03/20 Python
python数据结构学习之实现线性表的顺序
2018/09/28 Python
用python生成(动态彩色)二维码的方法(使用myqr库实现)
2019/06/24 Python
python中下标和切片的使用方法解析
2019/08/27 Python
Python输出指定字符串的方法
2020/02/06 Python
Python使用graphviz画流程图过程解析
2020/03/31 Python
如何把外网python虚拟环境迁移到内网
2020/05/18 Python
python Cartopy的基础使用详解
2020/11/01 Python
食品营养与检测应届生求职信
2013/11/08 职场文书
二手房购房协议书范本
2014/10/05 职场文书
介绍信的写法
2015/01/31 职场文书
成绩单家长意见
2015/06/03 职场文书
《女娲补天》教学反思
2016/02/20 职场文书
Mysql 如何查询时间段交集
2021/06/08 MySQL
科学家测试在太空中培育人造肉,用于未来太空旅行
2022/04/29 数码科技
MySql中的json_extract函数处理json字段详情
2022/06/05 MySQL