为什么删除购物车商品的时候不用给 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的识别的?

《L04 微信小程序从零到发布》
从小程序个人账户申请开始,带你一步步进行开发一个微信小程序,直到提交微信控制台上线发布。
《G01 Go 实战入门》
从零开始带你一步步开发一个 Go 博客项目,让你在最短的时间内学会使用 Go 进行编码。项目结构很大程度上参考了 Laravel。
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年前 评论

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