为什么删除购物车商品的时候不用给 where 传递 user->id???

$user->cartItems()->where([
'product_sku_id' => $productSku->id,
])
上面代码用tosql打印出来的是这样的:
select * from cart_items where cart_items.user_id = ? and cart_items.user_id is not null and (user_id = ? and product_sku_id is null)

里面没有和user表关联的地方,为什么不写成
$user->cartItems()->where([
'user_id' => $user->id,
'product_sku_id' => $productSku->id,
])

这样呢?
是怎样实现对user_id的识别的?

《L01 基础入门》
我们将带你从零开发一个项目并部署到线上,本课程教授 Web 开发中专业、实用的技能,如 Git 工作流、Laravel Mix 前端工作流等。
《L03 构架 API 服务器》
你将学到如 RESTFul 设计风格、PostMan 的使用、OAuth 流程,JWT 概念及使用 和 API 开发相关的进阶知识。
leo
最佳答案

@jingzhognwa 我通常会把 SQL 打印到日志了,进阶的课程里有介绍:9.7. 减少 SQL 查询(上)

5年前 评论
讨论数量: 3
leo

关联关系在这里 $user->cartItems()

5年前 评论

@leo
toSql()方法打印出来的select * from cart_items where cart_items.user_id = ? and cart_items.user_id is not null and (user_id = ? and product_sku_id is null)这个不是最终语句吗?
最终执行的SQL语句怎样获取?经常遇到想查看最终SQL语句的情况,但是都不知道有啥好方法。

5年前 评论
leo

@jingzhognwa 我通常会把 SQL 打印到日志了,进阶的课程里有介绍:9.7. 减少 SQL 查询(上)

5年前 评论

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!