成人精品一区二区三区中文字幕-成人精品一区二区三区-成人精品一级毛片-成人精品亚洲-日本在线视频一区二区-日本在线视频免费

導(dǎo)航首頁(yè) ? 技術(shù)教程 ? PHP中Laravel 關(guān)聯(lián)查詢返回錯(cuò)誤id的解決方法
全站頭部文字 我要出現(xiàn)在這里
PHP中Laravel 關(guān)聯(lián)查詢返回錯(cuò)誤id的解決方法 741 2024-01-14   

在 Laravel Eloquent 中使用 join 關(guān)聯(lián)查詢,如果兩張表有名稱(chēng)相同的字段,如 id,那么它的值會(huì)默認(rèn)被后來(lái)的同名字段重寫(xiě),返回不是期望的結(jié)果。例如以下關(guān)聯(lián)查詢:

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 字段,如果這樣構(gòu)造查詢的話,返回的查詢結(jié)果如圖:

查看圖片

Laravel 關(guān)聯(lián)查詢返回錯(cuò)誤的 id

這里 id 的值不是 priorities 表的 id 字段,而是 touch 表的 id 字段,如果打印出執(zhí)行的 sql 語(yǔ)句:

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

查詢結(jié)果如圖:

查看圖片

使用 sql 查詢的結(jié)果實(shí)際上是對(duì)的,另外一張表重名的 id 字段被默認(rèn)命名為 id1,但是 Laravel 返回的 id 的值卻不是圖中的 id 字段,而是被重名的另外一張表的字段重寫(xiě)了。

解決辦法是加一個(gè) select 方法指定字段,正確的構(gòu)造查詢語(yǔ)句的代碼:

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();

這樣就解決了問(wèn)題,那么以后就要注意了,Laravel 兩張表 join 的時(shí)候返回的字段最好要指定。

這算不算是 Laravel 的一個(gè) bug 呢?如果一個(gè)字段的值被同名的字段值重寫(xiě)了,這種情況要不要報(bào)一個(gè)錯(cuò)誤出來(lái),而不能默認(rèn)繼續(xù)執(zhí)行下去。

github 上有人也提出了同樣的問(wèn)題,作者也提供了解決辦法,但并沒(méi)其他更好的方案。

Laravel 版本:5.3

鏈接:https://github.com/laravel/framework/issues/4962

以上所述是小編給大家介紹的Laravel 關(guān)聯(lián)查詢返回錯(cuò)誤的 id的解決方法,希望對(duì)大家有所幫助,如果大家有任何疑問(wèn)請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)綠夏網(wǎng)網(wǎng)站的支持!



主站蜘蛛池模板: cctv6 节目表| dearestblue动漫免费观看| 影库影片| porn21| 张勇手演过的电影| 热天午后| 风月宝鉴电影剧情解析| 电影《地狱神探》| 陈宛蔚| 爱上美眉| 铁血使命演员表全部| 少年派2全集免费播放| 杨贵妃黄色片| 张柏芝艳照无删减版| 美女mm| 满天星的电影都有哪些| 女孩们的周末| 情侣视频| 日记的格式四年级| 小镇姑娘高清电影| 金马电影网| 少年班校花和富二代是什么情况| 姐妹们| 宁静是什么民族| 新上海滩张国荣和宁静| 黄祖蓝| 王卓淇| 张俪性感| 洗鼻器的使用方法教程| 伴生活| 第一序列第二季上映了吗| 田教授的28个保姆演员表| 含羞草传媒2024| 喜欢小红帽的原因怎么写| 欲望之事| 下一个奇迹| 采茶舞曲民乐合奏| 王风| 熊出没之雪岭熊风 2015 张伟 | 小马宝莉之小马国女孩| 抚摸视频|

!!!站長(zhǎng)長(zhǎng)期在線接!!!

網(wǎng)站、小程序:定制開(kāi)發(fā)/二次開(kāi)發(fā)/仿制開(kāi)發(fā)等

各種疑難雜癥解決/定制接口/定制采集等

站長(zhǎng)微信:lxwl520520

站長(zhǎng)QQ:1737366103