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查看session内容的函数
Aug 27 PHP
PHP 简单日历实现代码
Oct 28 PHP
一个基于PDO的数据库操作类
Mar 24 PHP
PHP中使用memcache存储session的三种配置方法
Apr 05 PHP
PHP实现的英文名字全拼随机排号脚本
Jul 04 PHP
php保存任意网络图片到服务器的方法
Apr 14 PHP
Laravel与CI框架中截取字符串函数
May 08 PHP
php四种定界符详解
Feb 16 PHP
php实现多维数组排序的方法示例
Mar 23 PHP
Yii框架中用response保存cookie,用request读取cookie的原理解析
Sep 04 PHP
Laravel 5.2 文档 数据库 ―― 起步介绍
Oct 21 PHP
Laravel5.5 视图 - 创建视图和数据传递示例
Oct 21 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
《APMServ 5.1.2》使用图解
2006/10/23 PHP
php中显示数组与对象的实现代码
2011/04/18 PHP
PhpMyAdmin出现export.php Missing parameter: what /export_type错误解决方法
2012/08/09 PHP
浅析PHP原理之变量(Variables inside PHP)
2013/08/09 PHP
CodeIgniter实现从网站抓取图片并自动下载到文件夹里的方法
2015/06/17 PHP
php获取远程文件大小
2015/10/20 PHP
详解PHP swoole process的使用方法
2017/08/26 PHP
php开发最强大的IDE编辑的phpstorm 2020.2配置Xdebug调试的详细教程
2020/08/17 PHP
收藏一些不常用,但是有用的代码
2007/03/12 Javascript
javascript window.confirm确认 取消对话框实现代码小结
2012/10/21 Javascript
javascript实现数字+字母验证码的简单实例
2014/02/10 Javascript
Javascript中引用示例介绍
2014/02/21 Javascript
js取整数、取余数的方法
2014/05/11 Javascript
javascript 10进制和62进制的相互转换
2014/07/31 Javascript
jquery获得同源iframe内body下标签的值的方法
2014/09/25 Javascript
jQuery前端框架easyui使用Dialog时bug处理
2014/12/05 Javascript
js获取表格的行数和列数的方法
2015/10/23 Javascript
全面解析Bootstrap表单使用方法(表单按钮)
2015/11/24 Javascript
javascript数据结构之双链表插入排序实例详解
2015/11/25 Javascript
react中的ajax封装实例详解
2017/10/17 Javascript
Node.js 实现简单的无侵入式缓存框架的方法
2019/07/21 Javascript
layui数据表格重载实现往后台传参
2019/11/15 Javascript
JavaScript中作用域链的概念及用途讲解
2020/08/06 Javascript
[01:56]2014DOTA2西雅图邀请赛 MVP外卡赛老队长精辟点评
2014/07/09 DOTA
[09:22]2014DOTA2西雅图国际邀请赛 主赛事第二日TOPPLAY
2014/07/21 DOTA
[46:04]Liquid vs VP Supermajor决赛 BO 第四场 6.10
2018/07/05 DOTA
opencv改变imshow窗口大小,窗口位置的方法
2018/04/02 Python
python写入已存在的excel数据实例
2018/05/03 Python
python3实现猜数字游戏
2020/12/07 Python
Space NK美国站:英国高端美妆护肤商城
2017/05/22 全球购物
如何将整数int转换成字串String
2014/03/21 面试题
.net面试题
2016/09/17 面试题
数据库测试通常都包括哪些方面
2015/11/30 面试题
什么是会话Bean
2015/05/14 面试题
说好普通话圆梦你我他演讲稿
2014/09/21 职场文书
企业年会祝酒词
2015/08/11 职场文书