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文件操作实例代码
May 10 PHP
php页码形式分页函数支持静态化地址及ajax分页
Mar 28 PHP
适用于抽奖程序、随机广告的PHP概率算法实例
Apr 09 PHP
php按百分比生成缩略图的代码分享
May 10 PHP
PHP防止post重复提交数据的简单例子
Jun 07 PHP
ThinkPHP3.1新特性之多层MVC的支持
Jun 19 PHP
Zend Framework教程之响应对象的封装Zend_Controller_Response实例详解
Mar 07 PHP
Zend Framework框架实现类似Google搜索分页效果
Nov 25 PHP
php 运算符与表达式详细介绍
Nov 30 PHP
Zend Framework入门应用实例详解
Dec 11 PHP
PHP对象相关知识总结
Apr 09 PHP
PHP中md5()函数的用法讲解
Mar 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
PHP4和PHP5性能测试和对比 测试代码与环境
2007/08/17 PHP
深入理解PHP原理之错误抑制与内嵌HTML分析
2011/05/02 PHP
实例讲解PHP表单处理
2019/02/15 PHP
使用Jquery搭建最佳用户体验的登录页面之记住密码自动登录功能(含后台代码)
2011/07/10 Javascript
js中对象的声明方式以及数组的一些用法示例
2013/12/11 Javascript
jquery事件preventDefault()方法用法实例
2015/01/16 Javascript
Jquery动态添加输入框的方法
2015/05/29 Javascript
页面向下滚动ajax获取数据的实现方法(兼容手机)
2016/05/24 Javascript
jQuery实现移动端手机商城购物车功能
2016/09/24 Javascript
AngularJS用户选择器指令实例分析
2016/11/04 Javascript
JS如何设置iOS中微信浏览器的title
2016/11/22 Javascript
基于vue2框架的机器人自动回复mini-project实例代码
2017/06/13 Javascript
Vue学习笔记进阶篇之函数化组件解析
2017/07/21 Javascript
js实现鼠标跟随运动效果
2020/08/02 Javascript
vue-star评星组件开发实例
2018/03/01 Javascript
详解Eslint 配置及规则说明
2018/09/10 Javascript
浅谈React之状态(State)
2018/09/19 Javascript
详解Node.js amqplib 连接 Rabbit MQ最佳实践
2019/01/24 Javascript
微信小程序云开发之使用云函数
2019/05/17 Javascript
详解vue-router的Import异步加载模块问题的解决方案
2020/05/13 Javascript
完美解决vue 中多个echarts图表自适应的问题
2020/07/19 Javascript
wxPython定时器wx.Timer简单应用实例
2015/06/03 Python
Python多线程经典问题之乘客做公交车算法实例
2017/03/22 Python
解决python flask中config配置管理的问题
2019/07/26 Python
Python pandas用法最全整理
2019/08/04 Python
Python队列RabbitMQ 使用方法实例记录
2019/08/05 Python
Python for i in range ()用法详解
2020/09/18 Python
python调用API接口实现登陆短信验证
2020/05/10 Python
使用matplotlib的pyplot模块绘图的实现示例
2020/07/12 Python
Python高并发解决方案实现过程详解
2020/07/31 Python
荷兰网上鞋店:Ziengs.nl
2017/01/02 全球购物
幼儿园教学随笔感言
2014/02/23 职场文书
幼儿园大班教师个人工作总结
2015/02/05 职场文书
2015初中政治教学工作总结
2015/07/21 职场文书
2016暑期社会实践新闻稿
2015/11/25 职场文书
MySQL系列之七 MySQL存储引擎
2021/07/02 MySQL