【发布时间】:2021-04-20 02:49:55
【问题描述】:
我试图找到以这种方式评论产品的用户:
public function getUserToRate(Product $product)
{
$userIds = DB::table('product_ratings')->where('product_id', $product->id)->pluck('user_id');
return $userIds;
}
但问题是 $product->id 为空。如果我这样做,就会出现这样的结果:
DB::table('product_ratings')->where('product_id', $product->id)->toSql():
select * from `product_ratings` where `product_id` = ?
为什么我的$product->id 为空?我已经检查并且模型存在,但我似乎无法在查询生成器中使用它。
谁能帮帮我?谢谢
【问题讨论】:
-
你怎么会认为是
null? -
因为在查询中它被标记为 ' ? ' 所以我假设它是一个空值,不是吗?
-
不是,是prepared statement的参数,sql语句和它的参数是分开发送到数据库的....查看“prepared statements”
标签: laravel laravel-query-builder