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
IIS+PHP+MySQL+Zend配置 (视频教程)
Dec 13 PHP
Discuz 模板引擎的封装类代码
Jul 18 PHP
php smarty函数扩展
Mar 15 PHP
PHP+Mysql+jQuery实现动态展示信息
Oct 08 PHP
三个类概括PHP的五种设计模式
Sep 05 PHP
作为PHP程序员应该了解MongoDB的五件事
Jun 03 PHP
php中mysql连接和基本操作代码(快速测试使用,简单方便)
Apr 25 PHP
PHP生成短网址的3种方法代码实例
Jul 08 PHP
mod_php、FastCGI、PHP-FPM等PHP运行方式对比
Jul 02 PHP
php的RSA加密解密算法原理与用法分析
Jan 23 PHP
宝塔面板出现“open_basedir restriction in effect. ”的解决方法
Mar 14 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的中问验证码
2006/11/25 PHP
PHP校验ISBN码的函数代码
2011/01/17 PHP
PHP上传图片到数据库并显示的实例代码
2019/12/20 PHP
Yii框架多语言站点配置方法分析【中文/英文切换站点】
2020/04/07 PHP
(仅IE下有效)关于checkbox 三态
2007/05/12 Javascript
用javascript实现画板的代码
2007/09/05 Javascript
javascript 自动填写表单的实现方法
2010/04/09 Javascript
兼容IE、FireFox、Chrome等浏览器的xml处理函数js代码
2011/11/30 Javascript
ECMAScript 创建自己的js类库
2012/11/22 Javascript
javascript实现的元素拖动函数宿主为浏览器
2014/07/21 Javascript
基于jQuery通过jQuery.form.js插件实现异步上传
2015/12/13 Javascript
修复jQuery tablesorter无法正确排序的bug(加千分位数字后)
2016/03/30 Javascript
分享12个非常实用的JavaScript小技巧
2016/05/11 Javascript
使用jQuery判断浏览器滚动条位置的方法
2016/05/30 Javascript
javascript验证手机号和实现星号(*)代替实例
2016/08/16 Javascript
Node.js connect ECONNREFUSED错误解决办法
2016/09/15 Javascript
Vue2.0 v-for filter列表过滤功能的实现
2018/09/07 Javascript
使用Vue实现移动端左滑删除效果附源码
2019/05/16 Javascript
[05:43]VG.R战队教练Mikasa专访:为目标从未停止战斗
2016/08/02 DOTA
[05:26]TI10典藏宝瓶套装外观展示
2020/07/03 DOTA
php使用递归与迭代实现快速排序示例
2014/01/23 Python
Python文件操作,open读写文件,追加文本内容实例
2016/12/14 Python
分数霸榜! python助你微信跳一跳拿高分
2018/01/08 Python
详解Python对JSON中的特殊类型进行Encoder
2019/07/15 Python
django基础学习之send_mail功能
2019/08/07 Python
Python Django view 两种return的实现方式
2020/03/16 Python
Python自动登录QQ的实现示例
2020/08/28 Python
HTML5本地存储之Database Storage应用介绍
2013/01/06 HTML / CSS
介绍下Java的输入输出流
2014/01/22 面试题
《欢乐的泼水节》教学反思
2014/04/22 职场文书
销售经理助理岗位职责
2015/04/13 职场文书
实施意见格式范本
2015/06/05 职场文书
新入职员工工作总结
2015/10/15 职场文书
2019年新郎保证书3篇
2019/10/17 职场文书
python 标准库原理与用法详解之os.path篇
2021/10/24 Python
mysql数据库实现设置字段长度
2022/06/10 MySQL