您好,以下是我的查询,但在运行时出现错误:
$out_let_product = OutLet::where('id', Redis::get('out_let_id') )->with(
['products' => function($query){
$query->with(['prices', 'combinations' => function($query){
$query->where('prices.active', '=', '1');
}]);
}])->get();在上面的代码中,“价格”和“组合”=产品模型中的函数。它工作得很好,但问题是当我包含“$query->where('prices.active','=','1');”这一行中,系统抛出的错误如下:
"message":"SQLSTATE42S22:未找到列: 1054 'where子句‘中的未知列'prices.active’(SQL: select * from product_combinations where product_combinations.product_id in (1,2,3) and prices.active =1 and product_combinations.deleted_at is null)",
发布于 2020-04-14 05:30:15
我猜combinations和prices引用了不同的表,并且您在combinations中使用了price.active。试着用这个
$out_let_product = OutLet::where('id', Redis::get('out_let_id') )->with(
['products' => function($query){
$query->with(['combinations' => function($query){
$query->select('combinations.*', 'prices.*')
->join('prices', function($join) {
$join->on('prices.id' '=', 'combination.id') // or your join logic
->where('prices.active', '=', 1)
})->where(// where clause for combination);
}])->get();发布于 2020-04-16 10:10:57
哦,是的,我有我的解决方案:
$out_let_product = OutLet::where('id', Redis::get('out_let_id') )->with(
['products' => function($query){
$query->with(['prices' => function($query){
$query->where('product_prices.active', '=', '1'); //product_prices is DB: Table name
}]);
$query->with(['combinations' => function($query){
query->where('product_combinations.id', '1'); //product_combinations is DB: Table name
}]);
}])->get();https://stackoverflow.com/questions/61195592
复制相似问题